7
Back to Problem Dungeon

Cache (LRU/LFU)

medium

Step 1: Requirements

Requirements

Questions to ask

  1. What eviction policy? (LRU, LFU, TTL, or configurable?)
  2. Fixed max size or dynamic?
  3. Is this an in-memory cache or backed by Redis/Memcached?
  4. Thread safety? (Node.js: single-threaded, but async operations matter)
  5. Cache-aside or read-through?
  6. Need cache stats (hit rate, eviction count)?
  7. Need cache invalidation API?

Functional requirements

  • get(key) — returns value or null (cache miss)
  • put(key, value, ttl?) — stores a value, evicts if at capacity
  • Configurable eviction: LRU (least recently used), LFU (least frequently used)
  • Optional TTL (time-to-live) per entry
  • delete(key) — manual invalidation
  • clear() — flush all entries

Non-functional requirements

  • O(1) get and put operations
  • Swappable eviction strategy without changing cache API
  • Observable: hit/miss/eviction stats
  • Transparent caching via Proxy pattern (optional)

1 / 7 — Requirements