Registry / storage / memory-cache-ttl

memory-cache-ttl

JSON →
library1.0.52jsnpmunverified

A simple, performant in-memory cache with configurable Time To Live (TTL) for Node.js. Current stable version is 1.0.52. It provides global and per-key TTL, automatic expiry cleanup via configurable interval, optional TTL randomization, extend-on-hit behavior, and an async onInterval callback to refresh cache entries. The package has zero dependencies and uses CommonJS. It is lightweight and minimal compared to more feature-rich caches like node-cache or lru-cache.

npm install memory-cache-ttl
INSTALL
IMPORT
SIG · MEMORY-CACHE-TTL
M
memory-cache-ttl
storagejavascriptv1.0.52
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
const cache = require('memory-cache-ttl');
import cache from 'memory-cache-ttl';
Package uses CommonJS only; no ESM exports.
init
cache.init({ ttl: 5 });
cache.init({ ttl: '5' });
TTL must be a number (seconds).
set
cache.set('key', 'value', 10);
cache.set('key', 'value', '10');
Optional TTL must be a number. If not provided, global TTL is used.
onInterval
cache.init({ onInterval: (id) => fetchNewValue(id) });
cache.init({ onInterval: fetchNewValue });
onInterval receives cache ID as argument, not event-like callback.

Initialize cache with 3-second TTL, set and get a value, then verify expiry after 5 seconds.

const cache = require('memory-cache-ttl'); cache.init({ ttl: 3, interval: 1, randomize: false }); cache.set('asd', 'dsa'); console.log(cache.check('asd')); // true console.log(cache.get('asd')); // 'dsa' setTimeout(() => { console.log(cache.check('asd')); // false console.log(cache.get('asd')); // undefined }, 5000);
Debug
Known issues
gotchaSetting TTL to 0 or negative disables expiry, causing entries to never expire.
fix
Use a positive integer for TTL. If you want no expiry, set TTL to Infinity or very large number.
affects: *
gotchaIf global TTL is not set (undefined), entries will never expire unless per-key TTL is provided.
fix
Always provide a global TTL or per-key TTL in set() to enable automatic expiry.
affects: *
gotchaThe onInterval callback must return a Promise; if no value is returned, the old value persists.
fix
Ensure onInterval returns a Promise that resolves to the new value or undefined to keep old value.
affects: *
gotchainterval setting controls cleanup interval, not per-entry TTL check; entries are only removed on interval ticks.
fix
Set interval to a value less than or equal to the smallest TTL for timely expiry.
affects: *
Errors
Common errors & fixes
cache.init is not a function
Incorrect import; package exports default object, not named export.
fix
Use const cache = require('memory-cache-ttl'); (CommonJS) or use default import if ESM wrapper exists (check package).
TypeError: cache.set is not a function
Imported default incorrectly or forgot to call init() first.
fix
Ensure cache is required as default: const cache = require('memory-cache-ttl'); then call cache.init() before set/get.
Value not expiring after TTL
Interval not set or set too high; cleanup may not have run yet.
fix
Set interval to a reasonable value (e.g., 1 second). Ensure cache.init({ interval: 1 }) is called.
Upgrade
Version history
1.0.52latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
12
Resources
memory-cache-ttl — npm install memory-cache-ttl · libregistry