node-lru-cache
A fast cache that automatically deletes the least recently used items
node-lru-cache is a JavaScript library that gives you a fast in-memory cache with automatic eviction. LRU stands for "least recently used," which describes the eviction rule: when the cache reaches its limit, the item that has not been accessed for the longest time is removed to make room for new entries. You use it when you want to keep frequently needed data close at hand without memory growing without bound.
You create a cache by passing a configuration object with at least one size constraint. The max option limits by item count and pre-allocates storage for that many slots when the cache is created, which makes reads and writes fast. The maxSize option limits by total storage, where each item provides its own size when stored, suitable when items vary significantly in size. You can also set a TTL in milliseconds, after which items are treated as absent when requested.
The library supports any type of key, not just strings. Object keys work, but lookup requires the exact same object reference, not just an object with matching properties. You can register a dispose callback that fires whenever an item leaves the cache, letting you release external resources like file handles or connections.
An async fetch mode lets you supply a function the cache calls when it needs to fill a missing entry. This supports stale-while-revalidate patterns, where a stale value is returned immediately while a fresh one is fetched in the background.
Tracing support is built in via Node.js diagnostics channels, allowing monitoring tools to observe cache activity. The library is available on npm and works in both Node.js and browser environments. It does not store undefined values, as that value is used internally to signal a missing key.