Skip to main content

Checkpoint: the bounded TTL cache

Challenge on lesson: Checkpoint: The Bounded TTL Cache

Inside Solution, build public static class Cache and static class CacheKey: 1. Backing store: WeakHashMap<CacheKey, Line> where static class Line carries String value and long expiry (System.nanoTime() + ttlNanos at put time). 2. void put(CacheKey k, String v, long ttlMillis) and String get(CacheKey k) — get returns null for missing keys AND for expired lines (expired lines are removed). 3. int size() — count of live (present, unexpired) entries; expires lazily on read. 4. void evictOver(int max) — while size() > max, remove the entry with the EARLIEST expiry (single pass over the entries is fine). 5. static String policyOf(Cache.class)-style reflection is NOT needed here — instead add static String describe() returning exactly: "time expires staleness; size bounds memory; weak keys release the dead".

Difficulty: advanced

Back to lesson: Checkpoint: The Bounded TTL Cache