← Back to Blog
Code Snippets August 14, 2026

Python: LRU Cache with TTL Eviction

🔧 Code Snippet of the Day — Aug 14, 2026

Python: LRU Cache with TTL Eviction

functools.lru_cache never expires. A dict with timestamps has no LRU eviction. This gives you both in ~100 lines of pure Python:

import time, threading
from collections import OrderedDict

class TTLCache:
    def __init__(self, maxsize=128, ttl=300.0):
        self.maxsize, self.ttl = maxsize, ttl
        self._cache = OrderedDict()
        self._lock = threading.RLock()
        self._hits = self._misses = self._evictions = 0

    def get(self, key, default=None):
        with self._lock:
            entry = self._cache.get(key)
            if entry is None:
                self._misses += 1
                return default
            value, created = entry
            if self.ttl > 0 and (time.monotonic() - created) >= self.ttl:
                del self._cache[key]; self._evictions += 1; self._misses += 1
                return default
            self._cache.move_to_end(key); self._hits += 1
            return value

    def set(self, key, value):
        with self._lock:
            if key in self._cache:
                self._cache.move_to_end(key)
            else:
                while len(self._cache) >= self.maxsize:
                    self._cache.popitem(last=False); self._evictions += 1
            self._cache[key] = (value, time.monotonic())


Usage:
cache = TTLCache(maxsize=100, ttl=60)
cache.set('user:123', {'name': 'Alice'})
print(cache.get('user:123'))  # {'name': 'Alice'}


Key features: O(1) ops via OrderedDict.move_to_end(), thread-safe, time.monotonic() for reliable expiration. Set ttl=0 for pure LRU, maxsize=float('inf') for pure TTL.

#python #caching #lru #ttl