Registry / storage / memory-cache

memory-cache

JSON →
library0.2.0jsnpmunverified

A simple in-memory cache for Node.js with support for TTL and expiration callbacks. Version 0.2.0 is stable with no recent releases; it uses setTimeout for expiration. Unlike more feature-rich solutions (e.g., node-cache, lru-cache), it offers a minimal API: put, get, del, clear, size, and import/export. Its constructor allows multiple isolated cache instances. Note that all operations are synchronous, and hits/misses are only tracked in debug mode.

npm install memory-cache
INSTALL
IMPORT
SIG · MEMORY-CACHE
M
memory-cache
storagejavascriptv0.2.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 instance
✓ const cache = require('memory-cache');
✗ import cache from 'memory-cache';
CommonJS-only; no default ES module export. Use require().
Cache class
✓ const { Cache } = require('memory-cache');
✗ const Cache = require('memory-cache').Cache;
Both patterns work, but destructuring is idiomatic.
importJson options
✓ cache.importJson(json, { skipDuplicates: true });
✗ cache.importJson(json, true);
Second argument must be an options object, not a boolean.

Demonstrates basic put/get, TTL with callback, cache expiration, and creating a separate Cache instance.

const cache = require('memory-cache'); // store a value indefinitely cache.put('foo', 'bar'); console.log(cache.get('foo')); // 'bar' // store with TTL (100ms) and expiration callback cache.put('houdini', 'disappear', 100, function(key, value) { console.log(key + ' did ' + value); }); console.log(cache.get('houdini')); // 'disappear' setTimeout(() => { console.log(cache.get('houdini')); // null (expired) }, 200); // create a separate cache instance const myCache = new cache.Cache(); myCache.put('key', 'value'); console.log(myCache.get('key')); // 'value'
Debug
Known issues
gotchaHits and misses are only tracked when debug mode is enabled via cache.debug(true).
fix
Enable debug mode: cache.debug(true); before checking hits/misses.
affects: >=0.0.1
gotchaTTL uses setTimeout internally; accuracy may be affected by Node.js event loop delays.
fix
Do not rely on exact millisecond expiration for timing-critical use cases.
affects: >=0.0.1
gotchaimportJson with skipDuplicates: false (default) overwrites duplicate keys silently.
fix
Pass skipDuplicates: true to preserve existing keys.
affects: >=0.1.0
deprecatedNo new releases since 2017; package is in maintenance mode.
fix
Consider alternatives like node-cache or lru-cache for active development.
affects: >0.2.0
Errors
Common errors & fixes
TypeError: cache.get is not a function
Requiring the module incorrectly, e.g. const cache = require('memory-cache').Cache; returns the class, not the default instance.
fix
Use const cache = require('memory-cache'); to get the default instance.
ReferenceError: require is not defined in ES module scope
Using import statement or running in ES module mode.
fix
Switch to CommonJS or use a dynamic import: const cache = await import('memory-cache'); (note: package is not ESM).
cache.put('key', 'value', 100, function(key, value) { ... }) but callback never called
TTL value must be a number in milliseconds; if omitted or non-number, callback is ignored.
fix
Ensure the third argument is a number (time in ms).
Upgrade
Version history
0.2.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
14
OpenAI (training)
1
Resources
memory-cache — npm install memory-cache · libregistry