Registry / storage / map-cache-ttl

map-cache-ttl

JSON →
library0.1.1jsnpmunverified

A Map subclass with TTL-based expiration for caching in Node.js (v0.1.1). It extends ES6 Map with optional per-key max age and automatic or manual trimming of expired entries. Supports proxy wrapping for memoization of async functions with cached results. Lightweight, no external dependencies required (except object-hash for proxy). Ideal for simple in-memory caching with fine-grained control over expiration.

npm install map-cache-ttl
INSTALL
IMPORT
SIG · MAP-CACHE-TTL
M
map-cache-ttl
storagejavascriptv0.1.1
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
const Cache = require('map-cache-ttl')
import Cache from 'map-cache-ttl' (default import not supported, use require or ESM with createRequire)
The package is CommonJS only. For ES modules, use createRequire.
constructor
new Cache(maxAge, interval)
new Cache({ maxAge: 5000, interval: 60000 }) (config object not supported)
Constructor takes two arguments: maxAge (number in ms) and interval (optional, number in ms for auto-trim).
proxy
const proxy = cache.proxy(fn, maxAge)
cache.proxy(fn, { maxAge: 5000 }) (options object not supported)
Second argument is optional per-proxy maxAge in ms.

Shows basic usage: creating a cache with 5s default TTL and 1s auto-trim, setting a key with 10s TTL, checking existence, and using proxy to memoize a fetch call for 30s.

const Cache = require('map-cache-ttl'); const cache = new Cache(5000, 1000); // default maxAge 5s, auto-trim every 1s cache.set('key', 'value', 10000); // expires in 10s console.log(cache.has('key')); // true setTimeout(() => console.log(cache.has('key')), 11000); // false after 11s const proxy = cache.proxy((url) => fetch(url).then(r => r.text()), 30000); proxy('https://example.com').then(data => console.log(data));
Debug
Known issues
gotchacache.size triggers cache.trim() internally, which may be O(n) and modifies internal state. Do not use size in performance-critical loops.
fix
Maintain your own counter or use cache.has(key) for specific keys.
affects: >=0.0.0
gotchaCalling cache.size or iterating after many insertions may not reflect accurate count if trim hasn't run; size forces trim.
fix
Use cache.trim() manually before reading size if you need precise counts without side effects.
affects: >=0.0.0
gotchaProxy function hashes arguments using object-hash; mutable objects will cause cache misses after mutation. Avoid mutable arguments.
fix
Serialize mutable arguments to stable strings before passing to proxy.
affects: >=0.0.0
gotchaExpired keys are not removed automatically unless trim() is called (or interval set). Memory leak if trim not called regularly.
fix
Always provide an interval in constructor or call trim() periodically.
affects: >=0.0.0
Errors
Common errors & fixes
Cannot find module 'map-cache-ttl'
Package not installed or import path incorrect in ESM context.
fix
npm install map-cache-ttl. For ESM, use const Cache = require('map-cache-ttl') via createRequire.
TypeError: cache.set is not a function
Using Cache as constructor without new (e.g., Cache('5s')). Extends Map, must be instantiated with new.
fix
Use 'new Cache(...)'.
Invalid max_age: must be a number
Passed string like '5s' instead of milliseconds. The library expects numeric milliseconds.
fix
Use numbers: new Cache(5000) for 5 seconds.
Upgrade
Version history
0.1.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

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