node-cache is a simple, fast, and internal in-memory caching module for Node.js applications, designed to function similarly to memcached but within a single Node.js process. It supports setting key-value pairs with optional time-to-live (TTL) expiration, allowing for automatic invalidation and deletion of stale data. The current stable version is 5.1.2, actively maintained with regular updates addressing bug fixes and minor features, as evidenced by recent 5.x releases. A major breaking change in v5.0.0 removed callback support, shifting entirely to synchronous operations (with an opt-in legacy callback option for migration), and dropped the `lodash` dependency, improving performance and reducing the bundle size. It differentiates itself by its straightforward API and focus on local process caching, suitable for scenarios where a full-fledged external caching solution like Redis or Memcached is overkill. Keys can be strings or numbers, and the cache can be configured with standard TTLs, periodic cleanup, and an option to store references vs. clones of values, balancing performance and data isolation.
npm install node-cacheVerified import paths — ran on the pinned version, not inferred.
Initializes NodeCache, sets and retrieves a value with a TTL, demonstrates handling missing keys, simulates caching an asynchronous operation, and shows the `take` method for atomic get-and-delete.
Rewrite code to use synchronous methods (e.g., `cache.get(key)` instead of `cache.get(key, callback)`). For a temporary migration path, initialize with `new NodeCache({ enableLegacyCallbacks: true })` but note this will be removed in v6.x.Upgrade your Node.js runtime to version 8.0.0 or higher. The recommended version is the latest LTS.
Review code that explicitly sets TTL to 0 or relies on `stdTTL=0` to ensure it's intended for unlimited expiration, not immediate deletion. For immediate deletion, use `.del(key)`.
Carefully consider the implications of `useClones`. For immutable data or read-only access, `true` (default) is safer. For performance-critical scenarios with mutable objects where you understand the risks, `false` can be used. Be aware that changing the default to `false` might introduce subtle bugs if not handled carefully.
Ensure all keys passed to `set`, `get`, `del`, etc., are either strings or numbers. Convert other types to strings if necessary before caching.
Avoid relying on `enableLegacyCallbacks`. Migrate your code to use the synchronous API calls as soon as possible to prepare for v6.x.
Remove the callback argument and use the synchronous return value: `const value = myCache.get(key);`
Convert your key to a string (e.g., `JSON.stringify(myObjectKey)` or `myObjectKey.id.toString()`) or ensure it's a number before passing it to cache methods.
Increase the `maxKeys` option during initialization (`new NodeCache({ maxKeys: desiredLimit })`), implement a custom eviction strategy by manually deleting less important keys, or handle the error gracefully.Upgrade to `node-cache` version 5.0.2 or newer to ensure `deleteOnExpire: false` functions as intended, allowing you to handle expired items manually via the 'expired' event.
No dependency data recorded yet.