Registry / storage / lru-cache-plus

lru-cache-plus

JSON →
library2.5.0jsnpmunverified

Extended version of Isaac's lru-cache 2.5.0. Adds per-item expiration (TTL) support via set(key, value, maxAge) and get() respects individual maxAge. Core LRU behavior retained: max size, length function, dispose callback, stale options, and forEach/keys/values iteration. Synchronous, zero dependencies, CommonJS only. Version 2.5.0 is stable but unmaintained; use the newer 'lru-cache' package (v4+) for continued updates and ESM support.

npm install lru-cache-plus
INSTALL
IMPORT
SIG · LRU-CACHE-PLUS
L
lru-cache-plus
storagejavascriptv2.5.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-cache-plus')
import LRU from 'lru-cache-plus'
Package is CommonJS only; ESM import will fail.
LRU with options
const cache = LRU({ max: 500, maxAge: 1000 * 60 })
const cache = new LRU({ max: 500 })
LRU is a factory function, not a constructor; do not use 'new'.
cache.set with TTL
cache.set('key', 'value', 5000)
cache.set('key', 'value', { maxAge: 5000 })
Third argument is numeric maxAge in ms, not an options object.

Demonstrates LRU creation with max and maxAge, setting items with per-item TTL, retrieving items (including stale handling), and iterating/resetting cache.

const LRU = require('lru-cache-plus'); const cache = LRU({ max: 100, maxAge: 1000 * 60 }); cache.set('key1', 'value1'); cache.set('key2', 'value2', 5000); // expires in 5 seconds console.log(cache.get('key1')); // 'value1' setTimeout(() => { console.log(cache.get('key1')); // undefined (expired) console.log(cache.get('key2')); // 'value2' (still valid) }, 70000); cache.forEach((val, key) => console.log(key, val)); cache.reset();
Debug
Known issues
deprecatedPackage is unmaintained; use 'lru-cache' v7+ for bug fixes and new features.
fix
Replace with 'lru-cache' (npm package) and adapt API (e.g., options are constructor-based, use cache.set(key, value, options) with ttl in ms).
affects: >=0.0.0
gotchadispose callback is called *before* item removal; re-inserting in dispose will be ignored without nextTick/setTimeout.
fix
Wrap re-insertion in process.nextTick(() => cache.set(key, value)) or setTimeout.
affects: >=0.0.0
gotchamaxAge is checked only on get(); stale items are not proactively pruned.
fix
Set stale: true to return stale value before deletion, or rely on periodic get() calls.
affects: >=0.0.0
gotchaLRU function expects different argument types: (options object) or (max number). Using 'new' keyword will break.
fix
Call as LRU(options) or LRU(maxNumber), never with 'new'.
affects: >=0.0.0
Errors
Common errors & fixes
LRU is not a constructor
Using 'new LRU()' when LRU is a factory function.
fix
Change to 'LRU(options)' without 'new'.
cache.set(...) is not a function
Using import statement with CommonJS-only package.
fix
Use 'const LRU = require('lru-cache-plus')' instead of ESM import.
TypeError: Cannot read property 'length' of undefined
Missing length function for objects without .length property.
fix
Provide custom length function, e.g., length: (n) => JSON.stringify(n).length.
Upgrade
Version history
2.5.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
21 hits · last 30 days
node
18
Amazon
1
OpenAI (training)
1
Resources
lru-cache-plus — npm install lru-cache-plus · libregistry