Key-value cache with per-entry TTL
Implement an in-memory key-value cache where every write gives a key, a value and a time-to-live. Once an entry's time is up it must behave as if it was never written. Support set, get and delete, and explain your choices as you go.
Follow-ups that come up:
-
getis the hot path, so keep it cheap and move cleanup of expired entries elsewhere (a background sweeper or sidecar). How do you find expired entries without scanning everything? - Add a capacity limit with least-recently-used eviction on top of the expiry.
- Make it safe for concurrent callers.
- Write and run tests, including ones that control the clock.
No answer yet. Write the first one; the best answer gets endorsed.
