Registry / storage / bluecache

bluecache

JSON →
library4.1.3jsnpmunverified

Bluecache is a read-through, in-memory, least recently used (LRU) cache for Node.js. Version 4.1.3 wraps the popular lru-cache library and adds a Promise-based API that combines get and set into a single call, preventing cache stampedes by immediately caching and returning a Promise for the priming value. It supports options like max, maxAge (with ms string parsing), stale, custom length, dispose, and a pruneInterval for proactive stale entry removal. The cache is lazily enforced and stores a memo while the value resolves. It has a stable release cadence and is suited for simple in-memory caching in Node.js applications.

npm install bluecache
INSTALL
IMPORT
SIG · BLUECACHE
B
bluecache
storagejavascriptv4.1.3
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
const Bluecache = require('bluecache');
import Bluecache from 'bluecache';
bluecache is a CommonJS module; ESM import may require bundler or conditional exports
cache instance
const cache = Bluecache({ max: 100 });
const cache = new Bluecache({ max: 100 });
Bluecache is a factory function, not a constructor; do not use `new`
cache options
const cache = Bluecache({ maxAge: '30m', pruneInterval: 60000 });
const cache = Bluecache(); cache.options = { maxAge: 60000 };
Options must be passed at instantiation; they cannot be set later

Demonstrates basic instantiation with options, read-through caching with Promise-returning function, cache hit/miss behavior, deletion, and reset.

const Bluecache = require('bluecache'); // Create a cache with max 500 items, max age of 1 hour const cache = Bluecache({ max: 500, maxAge: '1h' }); // Cache a value; the function will only be called if key is not cached cache('myKey', (key) => { console.log(`priming key: ${key}`); return Promise.resolve('myValue'); }).then(value => { console.log(value); // 'myValue' }); // Retrieve the value again (no priming call) cache('myKey', () => Promise.resolve('newValue')).then(value => { console.log(value); // 'myValue' (cached, so original value returned) }); // Delete a key cache.del('myKey').then(() => { console.log('deleted'); }); // Reset the entire cache cache.reset().then(() => { console.log('cache cleared'); });
Debug
Known issues
gotchaThe cache instance is a function, not an object with get/set methods. Calling cache(key, primingValue) performs a combined get/set.
fix
Use cache(key, primingValue) instead of a separate get/set pattern.
affects: >=4.0.0
gotchamaxAge is lazily enforced by lru-cache; expired keys may remain in memory until accessed. Use pruneInterval to proactively remove stale entries.
fix
Pass pruneInterval option (in ms) to the cache constructor to periodically clean expired entries.
affects: >=4.0.0
gotchaThe length of a cached item is temporarily 1 while its promise is pending. Peak cache size may exceed the specified max during resolution.
fix
Allow headroom in the max setting or monitor pending promises to avoid excessive memory usage.
affects: >=4.0.0
gotchaBluecache is a factory function; using `new Bluecache()` will throw an error or return unexpected results.
fix
Call Bluecache(options) without `new`.
affects: >=4.0.0
breakingIn version 4.x, the API changed from an object with get/set methods to a callable function. This is a major breaking change.
fix
Migrate from `cache.get(key).then(...)` to `cache(key, primingValue).then(...)`. Refer to the readme for new API.
affects: >=4.0.0 <5.0.0
Errors
Common errors & fixes
TypeError: Bluecache is not a constructor
Using `new Bluecache()` instead of calling Bluecache() directly.
fix
Change to `const cache = Bluecache(options);`
Error: maxAge must be a number or a string parsable by ms
Passing an invalid maxAge format, e.g., a number without unit or misspelled unit.
fix
Use a valid ms string like '1h', '30m', or a number in milliseconds.
TypeError: cache is not a function
Attempting to call cache.get() or cache.set() as methods, but bluecache instance is a function.
fix
Use cache(key, primingValue) directly; for delete and reset use cache.del() and cache.reset().
Upgrade
Version history
4.1.3latest on npm
Audit
Dependencies
lru-cacherequiredbluecache wraps lru-cache to provide the underlying LRU cache functionality
msrequiredused to parse human-readable maxAge strings like '1h' into milliseconds
Agent activity
42 hits · last 30 days
node
36
Amazon
1
Resources
bluecache — npm install bluecache · libregistry