Designing a TTL cache
advanced18 min readLesson 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.