Bounded caches (LRU)
LRU Map pattern
Section titled “LRU Map pattern”export function createLru(max = 500) { const map = new Map();
return { get(key) { if (!map.has(key)) return undefined; const val = map.get(key); map.delete(key); map.set(key, val); return val; }, set(key, val) { if (map.has(key)) map.delete(key); map.set(key, val); if (map.size > max) { map.delete(map.keys().next().value); } }, get size() { return map.size; }, };}TTL layer
Section titled “TTL layer”function setWithTtl(cache, key, val, ttlMs) { cache.set(key, { val, exp: Date.now() + ttlMs });}
function getWithTtl(cache, key) { const entry = cache.get(key); if (!entry) return undefined; if (Date.now() > entry.exp) { cache.delete(key); return undefined; } return entry.val;}Libraries
Section titled “Libraries”lru-cache(npm) — battle-tested for Node- Redis / Memcached — process-external bounds
flowchart LR
req[Request] --> cache{In cache?}
cache -->|yes| hit[Return cached]
cache -->|no| miss[Compute]
miss --> evict[Evict oldest if full]
evict --> store[Store]