{"id":46638,"library":"tmp-cache","title":"tmp-cache","description":"A minimal least-recently-used (LRU) cache implementation in about 35 lines of code, extending the native Map class. At version 1.1.0 (latest), it supports configurable max size, per-item or global maxAge for expiration, and a stale option to return expired values before deletion. Released steadily since 2017, it has zero dependencies and ships TypeScript type declarations. Compared to heavier alternatives like lru-cache, tmp-cache prioritizes simplicity and small bundle size, making it suitable for lightweight caching in Node.js (>=6) or modern browsers.","status":"active","version":"1.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/lukeed/tmp-cache","tags":["javascript","cache","lru","lru-cache","mru","typescript"],"install":[{"cmd":"npm install tmp-cache","lang":"bash","label":"npm"},{"cmd":"yarn add tmp-cache","lang":"bash","label":"yarn"},{"cmd":"pnpm add tmp-cache","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"ESM default export; CommonJS require returns the constructor directly, so require('tmp-cache') works without .default.","wrong":"const Cache = require('tmp-cache').default","symbol":"Cache (default)","correct":"import Cache from 'tmp-cache'"},{"note":"CommonJS usage via require; the module exports a single class, not a named export.","wrong":"const { Cache } = require('tmp-cache')","symbol":"Cache (CommonJS)","correct":"const Cache = require('tmp-cache')"},{"note":"For TypeScript, you can import the class as a value or use type-only import for type annotations.","wrong":null,"symbol":"Cache (TypeScript type)","correct":"import type { Cache } from 'tmp-cache'"}],"quickstart":{"code":"import Cache from 'tmp-cache';\n\n// Cache with max 3 items\nconst cache = new Cache(3);\ncache.set('a', 1);\ncache.set('b', 2);\ncache.set('c', 3);\ncache.get('a'); // refreshes expiry, moves to end\ncache.set('d', 4); // evicts oldest (b)\nconsole.log(cache.has('b')); // false\nconsole.log(cache.size); // 3\n\n// With maxAge (10ms) and stale allowed\nconst ageCache = new Cache({ maxAge: 10, stale: true });\nageCache.set('foo', 'bar', 20); // custom maxAge\nsetTimeout(() => {\n  console.log(ageCache.get('foo')); // 'bar' (stale, then removed)\n  console.log(ageCache.get('foo')); // undefined\n}, 15);","lang":"typescript","description":"Basic LRU cache creation with max size, get, set, size, has, and expiration with stale mode."},"warnings":[{"fix":"To set other options like maxAge, always pass an object: `new Cache({ max: 5, maxAge: 1000 })`.","message":"The options parameter can be an object or an integer. Passing an integer is treated as `max`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Call `cache.get(key, false)` to retrieve without refreshing expiry.","message":"The `get` method by default refreshes the item's expiration (resets maxAge timer). Use the `mutate` option to prevent refresh.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Do not rely on the cache to free memory automatically; call `delete` or `clear` explicitly if needed.","message":"Expired items are not proactively purged; they are only removed on access or when evicted by max size.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Pass `undefined` or omit the third argument to use the default maxAge.","message":"The `set` method accepts an optional third argument `maxAge` that overrides the instance's maxAge for that item.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"No action needed.","message":"The package does not have deprecated features per se, but the API is stable and unlikely to change.","severity":"deprecated","affected_versions":"1.1.0"}],"env_vars":null,"search_vec":"'1.1.0':26 '2017':54 '35':15 '6':89 'altern':67 'browser':92 'bundl':79 'cach':3,11,71,74,86,94,98 'class':23 'code':18 'compar':64 'configur':30 'declar':63 'delet':50 'depend':58 'expir':40,47 'extend':19 'global':37 'heavier':66 'implement':12 'item':35 'javascript':93 'latest':27 'least':7 'least-recently-us':6 'lightweight':85 'like':68 'line':16 'lru':10,70,95,97 'lru-cach':69,96 'make':81 'map':22 'max':31 'maxag':38 'minim':5 'modern':91 'mru':99 'nativ':21 'node.js':88 'option':44 'per':34 'per-item':33 'priorit':75 'recent':8 'releas':51 'return':46 'ship':60 'simplic':76 'sinc':53 'size':32,80 'small':78 'stale':43 'steadili':52 'suitabl':83 'support':29 'tmp':2,73 'tmp-cach':1,72 'type':62 'typescript':61,100 'use':9 'valu':48 'version':25 'zero':57","created_at":"2026-06-07T13:00:54.556371+00:00","updated_at":"2026-06-07T13:00:54.556371+00:00","problems":[{"fix":"Use `import Cache from 'tmp-cache'` for ES modules, or `const Cache = require('tmp-cache')` for CommonJS.","cause":"Using ES module import with CommonJS-required style or bundler misconfiguration.","error":"TypeError: Class extends value #<Object> is not a constructor or null"},{"fix":"Run `npm install tmp-cache` and ensure tsconfig.json includes `\"moduleResolution\": \"node\"`.","cause":"Package not installed or TypeScript cannot resolve types.","error":"Cannot find module 'tmp-cache' or its corresponding type declarations."},{"fix":"Ensure maxAge is a number (milliseconds) or undefined. Example: `cache.set('key', value, 1000)`.","cause":"Passing a non-number (e.g., string) as maxAge to `set` or in options.","error":"tmp-cache: `maxAge` must be a number"}],"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/lukeed/tmp-cache#readme","github":"https://github.com/lukeed/tmp-cache","docs":null,"changelog":null,"pypi":null,"npm":"tmp-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}}