{"id":46517,"library":"stale-lru-cache","title":"stale-lru-cache","description":"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.","status":"active","version":"5.1.1","language":"javascript","source_language":"en","source_url":"https://github.com/cyberthom/stale-lru-cache","tags":["javascript","lru","cache","stale-while-revalidate","max-age","cache-control"],"install":[{"cmd":"npm install stale-lru-cache","lang":"bash","label":"npm"},{"cmd":"yarn add stale-lru-cache","lang":"bash","label":"yarn"},{"cmd":"pnpm add stale-lru-cache","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is CommonJS only; ESM import will fail. Use require() as shown.","wrong":"import Cache from 'stale-lru-cache';","symbol":"Cache","correct":"var Cache = require('stale-lru-cache');"},{"note":"Default import is correct for ESM (if using bundler that supports CJS interop), but named import is incorrect.","wrong":"import { Cache } from 'stale-lru-cache';","symbol":"Cache","correct":"import Cache from 'stale-lru-cache';"},{"note":"CommonJS var/const assign is standard.","wrong":"","symbol":"Cache","correct":"const Cache = require('stale-lru-cache');"}],"quickstart":{"code":"var Cache = require('stale-lru-cache');\n\nvar cache = new Cache({\n    maxSize: 100,\n    maxAge: 600,\n    staleWhileRevalidate: 86400,\n    revalidate: function (key, callback) {\n        // Simulate async fetch\n        setTimeout(function() {\n            callback(null, 'refreshed value for ' + key);\n        }, 100);\n    }\n});\n\ncache.set('key', 'value');\ncache.get('key'); // 'value'\n// After 600 seconds, get returns stale value and triggers revalidation","lang":"javascript","description":"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."},"warnings":[{"fix":"Use has() to check existence before get() if needed.","message":"The get() method returns undefined for non-existent keys, but returns stale values even after maxAge expiry if within staleWhileRevalidate window.","severity":"gotcha","affected_versions":"*"},{"fix":"Always pass callback(null, value, null) if no options.","message":"revalidate callback must invoke callback(null, value, options) with options as third argument for cache-control headers; omitting options may cause unexpected behavior.","severity":"gotcha","affected_versions":"*"},{"fix":"Use callback(error, value) pattern instead of callback(value, error) from earlier versions.","message":"In version 5.x, the wrap() method's callback signature changed; error is now the first argument.","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Ensure values are in seconds (e.g., 600 for 10 minutes).","message":"maxAge and staleWhileRevalidate now expect seconds, not milliseconds (common mistake).","severity":"breaking","affected_versions":">=5.0.0"}],"env_vars":null,"search_vec":"'5.1.1':39 'age':56,99 'allow':25 'background':37,68 'cach':4,8,15,26,49,54,92,101 'cache-control':48,100 'configur':60 'control':50,102 'custom':47 'differenti':64 'discard':84 'error':77 'fault':66 'fault-toler':65 'first':89 'header':51 'hide':71 'hit':81 'hit-ratio':80 'http':53 'implement':19 'in-memori':11 'item':27,61,88 'javascript':90 'key':63 'latenc':73 'latest':42 'least':85 'lru':3,7,14,91 'max':55,57,98 'max-ag':97 'memori':13 'node.js':17 'optim':79 'ratio':82 'recent':86 'refresh':34,72 'releas':44 'revalid':23,69,76,96 'serv':30 'shield':74 'size':58,62 'stabl':43 'stale':2,6,21,31,94 'stale-lru-cach':1,5 'stale-while-revalid':20,93 'strategi':24 'support':46 'toler':67 'use':87 'version':38","created_at":"2026-06-07T13:00:19.116846+00:00","updated_at":"2026-06-07T13:00:19.116846+00:00","problems":[{"fix":"Run 'npm install stale-lru-cache' in your project directory.","cause":"Package not installed or missing from node_modules.","error":"Cannot find module 'stale-lru-cache'"},{"fix":"Use 'var Cache = require('stale-lru-cache');' or default ESM import.","cause":"Using named import instead of default import/require.","error":"TypeError: Cache is not a constructor"},{"fix":"Ensure callback is invoked: callback(null, value, options);","cause":"revalidate function defined without calling callback properly.","error":"callback is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/cyberthom/stale-lru-cache#readme","github":"https://github.com/cyberthom/stale-lru-cache","docs":null,"changelog":null,"pypi":null,"npm":"stale-lru-cache","openapi_spec":null,"status_page":null,"smithery":null,"categories":["storage"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-07","next_check":"2026-09-05","install_tag":null}}