A high-performance LRU (Least Recently Used) cache for JavaScript. Current stable version is 11.5.1, with active development and frequent releases. Key differentiators: one of the most performant LRU implementations in JavaScript, supports both CommonJS and ES modules, TypeScript types included, and offers flexible options like max size with size calculation, TTL, dispose callbacks, fetch-method for stale-while-revalidate patterns. Notably requires at least one of max, ttl, or maxSize to prevent unbounded storage. Version 7+ saw major internal rewrites for performance; from v10, `fetch()` changed signature (removed stale boolean).
npm install lru-cacheNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Creates an LRU cache with max 500 items and 5-minute TTL, then sets and gets a string key-value pair.
Use allowStale: true in cache options instead of passing stale as a fetch() parameter.
If upgrading from v6, see migration guide: dispose must be synchronous; use disposeAfter for async cleanup.
Always set a max, ttl, or maxSize to prevent unbounded memory usage.
Ensure you use the same object reference for get() and set(). Use Map for value equality.
Use named import: const { LRUCache } = require('lru-cache') or import { LRUCache } from 'lru-cache'Set max to a positive integer (e.g., max: 500) or undefined.
Provide a function: sizeCalculation: (value, key) => { return 1 }No dependency data recorded yet.