@bybrave/node-cache2
v6.0.1
Published
Maintained node-cache fork: rewritten from CoffeeScript to modern ESM, zero runtime dependencies, Map-backed storage, generic TypeScript types. Drop-in for node-cache.
Maintainers
Readme
@bybrave/node-cache2
Maintained fork of node-cache — simple and fast in-memory caching.
Rewritten from CoffeeScript to modern ESM, zero runtime dependencies, Map-backed storage, dual ESM + CommonJS, and generic TypeScript types. Same API.
Install
npm install @bybrave/node-cache2Usage
import NodeCache from '@bybrave/node-cache2';
const cache = new NodeCache({ stdTTL: 100, checkperiod: 120 });
cache.set('myKey', { any: 'value' });
cache.get('myKey'); // { any: 'value' }CommonJS works too — require returns the class directly:
const NodeCache = require('@bybrave/node-cache2');Typed cache (#273)
The class takes an optional type parameter, so a typed cache infers get/set/take without per-call generics:
import NodeCache from '@bybrave/node-cache2';
interface User { id: number; name: string; }
const users = new NodeCache<User>();
users.set('u1', { id: 1, name: 'Ann' });
const u = users.get('u1'); // User | undefinedThe full API — get, mget, set, mset, fetch, del, take, ttl, getTtl, keys, has, getStats, flushAll, flushStats, close, and the set/del/expired/flush/flush_stats events — is unchanged from node-cache.
Options
stdTTL, checkperiod, useClones, deleteOnExpire, forceString, maxKeys, enableLegacyCallbacks — same defaults and meaning as node-cache.
What's changed vs [email protected]
| Issue | Change |
|---|---|
| #69 | Rewritten from CoffeeScript to modern ESM. Ships native ESM with a CommonJS build via the exports map. |
| #212, #286 | Storage is backed by a Map instead of a plain object — prototype-safe (a __proto__ key is just a key) and faster for churny caches. |
| #273 | The class is generic: new NodeCache<MyType>() types get/set/take/mget. Per-call type parameters still work. |
| #230 | Zero runtime dependencies — clone is vendored and maintained in-tree, so there is no transitive dependency to audit. |
Migration from node-cache
Drop-in — replace the import:
- const NodeCache = require('node-cache');
+ const NodeCache = require('@bybrave/node-cache2');The public API and option defaults are unchanged. Notes for edge cases:
keys()still returns strings, and numeric/string keys still address the same entry (set(5, …)thenget('5')), matching the original object-backed behaviour.- With
useClones: true(the default), values are deep-cloned with the vendoredclone, so objects with methods, circular references,Map/Set/Date/RegExp/Bufferall clone the same way as before.
Support
If this package saves you time, you can support maintenance:
Bitcoin (BTC): bc1q37557q5jpeaxqydzwvf3jgj7zhnfpn2td3q40q
License
MIT. Copyright © mpneuried; vendored clone © Paul Vorbach, Blake Miner; fork © bybrave.
