Step 1: Requirements
Requirements
Questions to ask
- What eviction policy? (LRU, LFU, TTL, or configurable?)
- Fixed max size or dynamic?
- Is this an in-memory cache or backed by Redis/Memcached?
- Thread safety? (Node.js: single-threaded, but async operations matter)
- Cache-aside or read-through?
- Need cache stats (hit rate, eviction count)?
- 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)