Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Designing a TTL cache

โญโญโญ advancedโณ 18 min read๐Ÿ“ Lesson 151 of 180

WeakHashMap keys, time windows, and size bounds โ€” three mechanisms, three failures.

A GC-backed TTL cache binds three mechanisms:

  • WeakHashMap โ€” the collector retires entries whose keys are reachable only from the map. (Values must not reach their own keys, or nothing dies.)
  • A 60-second window โ€” System.nanoTime() arithmetic; expired by time.
  • A size bound โ€” eviction by policy.
boolean fresh(CacheKey k) {
    CacheLine line = store.get(k);
    if (line == null) return false;
    if (line.expiry() <= System.nanoTime()) { store.remove(k); return false; }
    return true;
}

Each mechanism answers a different failure: time expires stale data, size bounds memory, weak keys release objects whose owners are gone. Remove any one and the cache leaks in that dimension. The checkpoint assembles all three โ€” the same reasoning production caches (Caffeine and friends) apply at industrial strength.

โšก Now practice

Ready to Code
Memory drillsWatch the collector work, pin objects on purpose, and feel the boxed-cache boundary.
3 challenges ยท ยท ~55 min