Reading — step 1 of 5
Read
~1 min readConcurrency
LRU with TTL
Combining LRU eviction with time-based expiry:
python
TTL handling options:
Lazy — only check on access. Cheap; expired entries linger until evicted normally.
Active — background thread periodically scans for expired. Predictable memory; CPU cost.
Sorted by expiry — maintain a min-heap by expires_at. O(log n) for insertion; O(1) to peek the next-to-expire. Used by Redis-style sorted-set TTL.
Most caches do lazy + occasional sweeps. Memcached, Redis, Caffeine all blend.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…