Registry / storage / tmp-cache

tmp-cache

JSON →
library1.1.0jsnpmunverified

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.

npm install tmp-cache
INSTALL
IMPORT
SIG · TMP-CACHE
T
tmp-cache
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 (default)
✓ import Cache from 'tmp-cache'
✗ const Cache = require('tmp-cache').default
ESM default export; CommonJS require returns the constructor directly, so require('tmp-cache') works without .default.
Cache (CommonJS)
✓ const Cache = require('tmp-cache')
✗ const { Cache } = require('tmp-cache')
CommonJS usage via require; the module exports a single class, not a named export.
Cache (TypeScript type)
✓ import type { Cache } from 'tmp-cache'
For TypeScript, you can import the class as a value or use type-only import for type annotations.

Basic LRU cache creation with max size, get, set, size, has, and expiration with stale mode.

import Cache from 'tmp-cache'; // Cache with max 3 items const cache = new Cache(3); cache.set('a', 1); cache.set('b', 2); cache.set('c', 3); cache.get('a'); // refreshes expiry, moves to end cache.set('d', 4); // evicts oldest (b) console.log(cache.has('b')); // false console.log(cache.size); // 3 // With maxAge (10ms) and stale allowed const ageCache = new Cache({ maxAge: 10, stale: true }); ageCache.set('foo', 'bar', 20); // custom maxAge setTimeout(() => { console.log(ageCache.get('foo')); // 'bar' (stale, then removed) console.log(ageCache.get('foo')); // undefined }, 15);
Debug
Known issues
gotchaThe options parameter can be an object or an integer. Passing an integer is treated as `max`.
fix
To set other options like maxAge, always pass an object: `new Cache({ max: 5, maxAge: 1000 })`.
affects: >=1.0.0
gotchaThe `get` method by default refreshes the item's expiration (resets maxAge timer). Use the `mutate` option to prevent refresh.
fix
Call `cache.get(key, false)` to retrieve without refreshing expiry.
affects: >=1.0.0
gotchaExpired items are not proactively purged; they are only removed on access or when evicted by max size.
fix
Do not rely on the cache to free memory automatically; call `delete` or `clear` explicitly if needed.
affects: >=1.0.0
gotchaThe `set` method accepts an optional third argument `maxAge` that overrides the instance's maxAge for that item.
fix
Pass `undefined` or omit the third argument to use the default maxAge.
affects: >=1.0.0
deprecatedThe package does not have deprecated features per se, but the API is stable and unlikely to change.
fix
No action needed.
affects: 1.1.0
Errors
Common errors & fixes
TypeError: Class extends value #<Object> is not a constructor or null
Using ES module import with CommonJS-required style or bundler misconfiguration.
fix
Use `import Cache from 'tmp-cache'` for ES modules, or `const Cache = require('tmp-cache')` for CommonJS.
Cannot find module 'tmp-cache' or its corresponding type declarations.
Package not installed or TypeScript cannot resolve types.
fix
Run `npm install tmp-cache` and ensure tsconfig.json includes `"moduleResolution": "node"`.
tmp-cache: `maxAge` must be a number
Passing a non-number (e.g., string) as maxAge to `set` or in options.
fix
Ensure maxAge is a number (milliseconds) or undefined. Example: `cache.set('key', value, 1000)`.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
22 hits · last 30 days
node
22
Resources
tmp-cache — npm install tmp-cache · libregistry