Registry / storage / lru-cache-node

lru-cache-node

JSON →
library1.1.0jsnpmunverified

A lightweight, high-performance LRU cache for Node.js using a doubly-linked list and hash map for O(1) get/set operations. Version 1.1.0 supports optional per-key expiry (TTL), returning stale values before eviction, and methods like peek, delete, has, forEach, and toArray. Compared to alternatives like lru-cache, lru-cache-node has a simpler API, lower overhead for small caches, and no external dependencies. It is suitable for minimal caching needs in Node.js applications.

npm install lru-cache-node
INSTALL
IMPORT
SIG · LRU-CACHE-NODE
L
lru-cache-node
storagejavascriptv1.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.

Cache
const Cache = require('lru-cache-node');
import Cache from 'lru-cache-node';
Package is CommonJS-only; no native ESM support.
Cache
import Cache from 'lru-cache-node';
If using ESM with bundlers/synthetic defaults, this may work depending on environment, but not officially supported.
LRUCache
const LRUCache = require('lru-cache-node').LRUCache;
const { LRUCache } = require('lru-cache-node');
The package does not export LRUCache; use the default export.

Demonstrates basic usage: creating a cache, setting/getting values with optional TTL, peeking, deleting, checking existence, iterating, and converting to array.

const Cache = require('lru-cache-node'); // Create a cache with max size 3 and default TTL 10000ms const cache = new Cache(3, 10000); // Set keys with optional per-key TTL (in ms) cache.set('a', 1); cache.set('b', 2, 5000); // expires in 5 seconds cache.set('c', 3); // Get a value console.log(cache.get('a')); // 1 // Peek without affecting LRU order console.log(cache.peek('b')); // 2 // Delete a key cache.delete('c'); // Check existence console.log(cache.has('a')); // true // Iterate using forEach cache.forEach((key, value, index) => { console.log(`${key}: ${value} at index ${index}`); }); // Convert to array console.log(cache.toArray()); // [ { key: 'b', value: 2 }, { key: 'a', value: 1 } ]
Debug
Known issues
gotchaThe default maxSize is Infinity if not provided; cache will never evict entries.
fix
Always specify maxSize to avoid unbounded growth: new Cache(maxSize).
affects: *
gotchaExpired keys are only cleaned up on access (get/peek/contains). Cache memory is not reclaimed passively.
fix
Use set() to overwrite or reset()/delete() to clear manually. Consider a proactive cleanup mechanism for long-running apps.
affects: *
gotchastale option only works when maxAge is set; otherwise expired keys are never returned.
fix
If using stale: true, ensure cache is initialized with maxAge (even if large).
affects: *
deprecatedThe method name 'contains' is deprecated; use 'has' instead.
fix
Replace calls to cache.contains(key) with cache.has(key).
affects: >=1.0.0
gotchaThe toArray() method returns entries with properties 'key' and 'value', not standard Map format.
fix
Iterate using toArray().forEach(({key, value}) => ...) or use the built-in forEach method.
affects: *
Errors
Common errors & fixes
TypeError: Cache is not a constructor
The default export is a constructor function; it is often mistaken for a named export or imported incorrectly.
fix
Use const Cache = require('lru-cache-node'); then new Cache(maxSize, maxAge, stale);
Cannot read property 'get' of undefined
Cache instance is not properly created (e.g., missing new keyword).
fix
Ensure you instantiate with new: const cache = new Cache(100);
TypeError: cache.set is not a function
CommonJS default export mistaken for a named export or imported as an object.
fix
Import correctly: const Cache = require('lru-cache-node'); const cache = new Cache();
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources
lru-cache-node — npm install lru-cache-node · libregistry