Registry / storage / ttl-mem-cache

ttl-mem-cache

JSON →
library4.1.0jsnpmunverified

An in-memory TTL key/value cache with streaming support. Current stable version is 4.1.0. It does not proactively prune expired items; instead, items expire when touched, avoiding setTimeout overhead. There is no maximum item limit, making it suitable for small to medium datasets. Key differentiators include per-item TTL, stale data delivery option, change event streaming via a Duplex stream, and an optional instance ID for origin tracking. It is a Node.js-only library, not browser-compatible.

npm install ttl-mem-cache
INSTALL
IMPORT
SIG · TTL-MEM-CACHE
T
ttl-mem-cache
storagejavascriptv4.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.

default (the Cache class)
import Cache from 'ttl-mem-cache';
const { Cache } = require('ttl-mem-cache');
Package exports a single default class; named destructure fails. Use default import.
Cache (via require)
const Cache = require('ttl-mem-cache');
const cache = new Cache(); // missing require
CommonJS style works fine for older code; no named exports.
Cache (ESM with namespace)
import Cache from 'ttl-mem-cache';
import * as Cache from 'ttl-mem-cache';
Namespace import gives an object with default property; use default import instead.

Shows basic instantiation with TTL options, setting/getting values, and stale mode behavior.

const Cache = require('ttl-mem-cache'); const cache = new Cache({ ttl: 60000 }); // 1 minute default TTL // Set a value with default TTL cache.set('example', { message: 'Hello, world!' }); // Set a value with custom TTL cache.set('ephemeral', 'data', 5000); // expires in 5 seconds // Get value const value = cache.get('example'); console.log(value); // { message: 'Hello, world!' } // Get after TTL expiration setTimeout(() => { const expiredValue = cache.get('example'); console.log(expiredValue); // null (if not stale) }, 61000); // Get stale value const staleCache = new Cache({ ttl: 10000, stale: true }); staleCache.set('staleItem', 'old data'); setTimeout(() => { const stale = staleCache.get('staleItem'); console.log(stale); // 'old data' (even though expired, returned before removal) }, 11000);
Debug
Known issues
breakingIn v2.0.0, the constructor signature changed: options object is required instead of separate arguments.
fix
Update instantiation to new Cache({ ttl: ... }) instead of new Cache(ttl, stale).
affects: <2.0.0
deprecatedAs of v3.0.0, .remove() method is deprecated; use .del() instead.
fix
Replace cache.remove(key) with cache.del(key).
affects: >=3.0.0
gotchaThe cache does not proactively expire items; expired items are only removed when accessed. Memory may grow if items are never touched.
fix
Periodically call .get() or .entries() to trigger pruning, or implement a separate cleanup loop.
affects: >=0.0.0
gotchaStreaming events (set, del, expire) are emitted on the Duplex stream; forgetting to pipe or listen can cause memory backpressure issues.
fix
If using stream mode, ensure you consume the readable side or handle backpressure properly.
affects: >=4.0.0
gotchaSetting ttl to Infinity caches forever, but the item can still be overwritten or deleted. This can be unexpected if you assume no changes.
fix
Use a sufficiently large number (e.g., Number.MAX_SAFE_INTEGER) if you want a clear expiration boundary.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: cache.get is not a function
Importing incorrectly; using named import instead of default.
fix
Use default import: const Cache = require('ttl-mem-cache'); or import Cache from 'ttl-mem-cache';
TypeError: cache.set is not a function
Trying to call method on wrong object, e.g., calling set on the class instead of an instance.
fix
Instantiate first: const cache = new Cache(); then cache.set(key, value);
Uncaught ReferenceError: require is not defined
Using CommonJS require in an ES module environment.
fix
Use import Cache from 'ttl-mem-cache'; instead of require.
Upgrade
Version history
4.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
29 hits · last 30 days
node
26
OpenAI (training)
1
Resources
ttl-mem-cache — npm install ttl-mem-cache · libregistry