Registry / storage / lru
library3.1.0jsnpmunverified

A minimal O(1) LRU cache for Node.js supporting set, get, peek, remove, clear, and eviction events. Version 3.1.0 is stable and suitable for small caches. It uses a linked list and hash map for O(1) operations. Differentiators: simple API, evict event, optional maxAge for TTL, and CommonJS support. No TypeScript types, no ESM. Suitable for Node >= 0.4.0.

npm install lru
INSTALL
IMPORT
SIG · LRU
L
lru
storagejavascriptv3.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

LRU
const LRU = require('lru');
import LRU from 'lru';
Package is CommonJS only; ESM import will fail.
LRU (with options)
const cache = new LRU({ max: 100, maxAge: 60000 });
const cache = new LRU(100, 60000);
Options must be passed as an object, not separate arguments.
evict event
cache.on('evict', function(data) { console.log(data.key, data.value); });
cache.on('evict', (data) => { console.log(data.key); });
Arrow functions are fine, but ensure 'this' context is not needed.

Creates an LRU cache with max 2 items, listens for evict event, and demonstrates set/get/remove/clear.

const LRU = require('lru'); const cache = new LRU(2); let evicted; cache.on('evict', function(data) { evicted = data; }); cache.set('foo', 'bar'); cache.get('foo'); // => 'bar' cache.set('foo2', 'bar2'); cache.set('foo3', 'bar3'); // evicted => { key: 'foo', value: 'bar' } console.log(cache.length); // 1 console.log(cache.keys); // ['foo3'] cache.remove('foo2'); // => 'bar2' cache.clear(); console.log(cache.length); // 0
Debug
Known issues
gotchaThe `length` property is not a function; it's a number.
fix
Access `cache.length` directly, not `cache.length()`.
affects: >=1.0.0
gotcha`cache.remove()` returns the value if key exists, else undefined.
fix
Check return value for existence: const val = cache.remove(key); if (val !== undefined) { ... }
affects: >=1.0.0
gotcha`cache.clear()` does NOT emit the 'evict' event.
fix
Manually emit evict events before clear if needed.
affects: >=1.0.0
gotchaThe `maxAge` option is in milliseconds, not seconds.
fix
Set `maxAge` in milliseconds.
affects: >=2.0.0
gotchaThe `get` method will return `undefined` for expired items (if maxAge set) without emitting evict.
fix
Use `peek` if you want to check without marking as recently used.
affects: >=2.0.0
deprecatedThe package has no TypeScript type definitions.
fix
Create own `.d.ts` or use `@types/lru` if available.
affects: >=1.0.0
gotchaConstructor accepts either a number (max items) or an options object.
fix
Pass options object with `max` and optionally `maxAge`.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: lru is not a function
Importing the package incorrectly (ESM import instead of require).
fix
Use `const LRU = require('lru');`
TypeError: cache.length is not a function
Calling `cache.length()` as a function, but it's a property.
fix
Use `cache.length` without parentheses.
ReferenceError: LRU is not defined
Capitalization error or missing require.
fix
Ensure `const LRU = require('lru');` is present.
TypeError: cache.on is not a function
Calling `on` before cache is initialized, or using a plain object.
fix
Create a new LRU instance first: `const cache = new LRU(10);`
Upgrade
Version history
3.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
18 hits · last 30 days
node
18
Resources
packagelru
lru — npm install lru · libregistry