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.

storage
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.

ESM default export; CommonJS require returns the constructor directly, so require('tmp-cache') works without .default.

import Cache from 'tmp-cache'

CommonJS usage via require; the module exports a single class, not a named export.

const Cache = require('tmp-cache')

For TypeScript, you can import the class as a value or use type-only import for type annotations.

import type { Cache } from 'tmp-cache'

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 footguns
gotchaThe options parameter can be an object or an integer. Passing an integer is treated as `max`.
gotchaThe `get` method by default refreshes the item's expiration (resets maxAge timer). Use the `mutate` option to prevent refresh.
gotchaExpired items are not proactively purged; they are only removed on access or when evicted by max size.
gotchaThe `set` method accepts an optional third argument `maxAge` that overrides the instance's maxAge for that item.
deprecatedThe package does not have deprecated features per se, but the API is stable and unlikely to change.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
11 hits · last 30 days
gptbot
3
amazonbot
1
Resources