Registry / storage / cached

cached

JSON →
library6.1.0jsnpmunverified

A simple caching library for Node.js (v6.1.0) inspired by the Play cache API, designed to prefer serving stale data over causing a stampede on the backend. Supports both promise- and callback-based usage. Built-in backends: in-memory (default) and memcached (via memcached-elasticache). Released by Groupon, follows semantic versioning. Key differentiators: minimal interface (no multi-get, no deletion), stale-while-revalidate bias, named caches with automatic key prefixes.

npm install cached
INSTALL
IMPORT
SIG · CACHED
C
cached
storagejavascriptv6.1.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.

cached
const cached = require('cached');
import cached from 'cached';
Package uses CommonJS, not ESM. Only require() works.
cached.deferred
const cached = require('cached'); cached.deferred(callbackFn);
import { deferred } from 'cached';
deferred is a static method on the cached function, not a named export.
cached.createCache
const cached = require('cached'); const cache = cached.createCache(options);
const cache = cached.createCache(options); // works but note options include name
Allows creating a cache without registering it globally. Options must include a 'name' property.

Demonstrates basic usage of cached: creating a named cache, setting/getting values synchronously, using getOrElse with a factory function, and using deferred for callback-style operations.

const cached = require('cached'); async function main() { // Create a named cache (in-memory default) const kittens = cached('kittens'); // Set a key await kittens.set('my.key', 'Hello World'); // Get a key (returns undefined if not found) const value = await kittens.get('my.key'); console.log(value); // 'Hello World' // Get or set with a factory function const data = await kittens.getOrElse('my.key', () => { return 'Fallback value'; }); console.log(data); // 'Hello World' (already cached) // Using a lazy promise in set await kittens.set('other.key', () => { return Promise.resolve('Lazy value'); }); // Using callback-style via deferred await kittens.set('callback.key', cached.deferred(done => { done(null, 'Callback value'); })); } main().catch(console.error);
Debug
Known issues
gotchaMemory backend get returns a reference to the stored object, not a clone. Mutating the returned value will modify the cache.
fix
Copy the value before mutation if needed: const val = await cache.get('key'); const copy = { ...val }
affects: >=6.0.0
deprecateddeferred method signature has changed; now expects a callback that receives (done) rather than (err, result).
fix
Use cached.deferred(done => { ... }) instead of cached.deferred(function(err, result) { ... })
affects: >=5.0.0
gotchaCache keys are prefixed with the cache name followed by a colon. If you manually construct keys, they may conflict.
fix
Use only the logical key (without prefix) when calling get/set. The prefix is internal.
affects: >=6.0.0
deprecatedThe global named cache registry may cause memory leaks if caches are not dropped.
fix
Use cached.createCache(options) for temporary caches and call cached.dropNamedCache(name) when done.
affects: >=6.0.0
Errors
Common errors & fixes
cached is not a function
Using ES module import (import cached from 'cached') in a CommonJS environment or vice versa.
fix
Use const cached = require('cached'); instead.
TypeError: done is not a function
Using older deferred signature (done(err, result)) instead of (done) => done(err, result).
fix
cached.deferred(done => { done(null, value); })
Cannot find module 'memcached-elasticache'
Using memcached backend without installing the optional dependency.
fix
npm install memcached-elasticache
TypeError: cache.get is not a function
cache.getOrElse expects a function as second argument; passing a plain value.
fix
cache.getOrElse('key', () => value) or use cache.set('key', value) first.
Upgrade
Version history
6.1.0latest on npm
Audit
Dependencies
memcached-elasticacheoptionalRequired when backend type is 'memcached'
Agent activity
29 hits · last 30 days
node
28
Resources
packagecached
cached — npm install cached · libregistry