Registry / storage / stale-lru-cache

stale-lru-cache

JSON →
library5.1.1jsnpmunverified

stale-lru-cache is an in-memory LRU cache for Node.js that implements stale-while-revalidate strategy, allowing cached items to be served stale while being refreshed in the background. Version 5.1.1 is the latest stable release. It supports custom Cache-Control headers for HTTP caching, max age, max size, and configurable item sizing. Key differentiators: fault-tolerant background revalidation that hides refresh latency, shields from revalidation errors, and optimizes hit-ratio by discarding least recently used items first.

npm install stale-lru-cache
INSTALL
IMPORT
SIG · STALE-LRU-CACHE
S
stale-lru-cache
storagejavascriptv5.1.1
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
✓ var Cache = require('stale-lru-cache');
✗ import Cache from 'stale-lru-cache';
This package is CommonJS only; ESM import will fail. Use require() as shown.
Cache
✓ import Cache from 'stale-lru-cache';
✗ import { Cache } from 'stale-lru-cache';
Default import is correct for ESM (if using bundler that supports CJS interop), but named import is incorrect.
Cache
✓ const Cache = require('stale-lru-cache');
CommonJS var/const assign is standard.

Creates a stale-while-revalidate cache with maxSize 100, 10 min maxAge, 24h stale window, and a revalidation function. Demonstrates set/get with synchronous initial data.

var Cache = require('stale-lru-cache'); var cache = new Cache({ maxSize: 100, maxAge: 600, staleWhileRevalidate: 86400, revalidate: function (key, callback) { // Simulate async fetch setTimeout(function() { callback(null, 'refreshed value for ' + key); }, 100); } }); cache.set('key', 'value'); cache.get('key'); // 'value' // After 600 seconds, get returns stale value and triggers revalidation
Debug
Known issues
gotchaThe get() method returns undefined for non-existent keys, but returns stale values even after maxAge expiry if within staleWhileRevalidate window.
fix
Use has() to check existence before get() if needed.
affects: *
gotcharevalidate callback must invoke callback(null, value, options) with options as third argument for cache-control headers; omitting options may cause unexpected behavior.
fix
Always pass callback(null, value, null) if no options.
affects: *
breakingIn version 5.x, the wrap() method's callback signature changed; error is now the first argument.
fix
Use callback(error, value) pattern instead of callback(value, error) from earlier versions.
affects: >=5.0.0
breakingmaxAge and staleWhileRevalidate now expect seconds, not milliseconds (common mistake).
fix
Ensure values are in seconds (e.g., 600 for 10 minutes).
affects: >=5.0.0
Errors
Common errors & fixes
Cannot find module 'stale-lru-cache'
Package not installed or missing from node_modules.
fix
Run 'npm install stale-lru-cache' in your project directory.
TypeError: Cache is not a constructor
Using named import instead of default import/require.
fix
Use 'var Cache = require('stale-lru-cache');' or default ESM import.
callback is not a function
revalidate function defined without calling callback properly.
fix
Ensure callback is invoked: callback(null, value, options);
Upgrade
Version history
5.1.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
16 hits · last 30 days
node
16
Resources
stale-lru-cache — npm install stale-lru-cache · libregistry