Registry / database / locking-cache

locking-cache

JSON →
library0.4.1jsnpmunverified

A locking LRU cache that deduplicates concurrent calls to a cached function: if multiple requests arrive for the same key while the initial call is in-flight, they all wait and receive the cached result once available. Current version 0.4.1 wraps lru-cache to provide a lock/unlock mechanism. The package is minimal but stable, with no recent releases since 2016 (likely maintenance mode). Key differentiators: integrates locking semantics on top of any compatible LRU cache, supports custom factory functions for alternative cache backends, and passes varargs through to all pending callbacks.

npm install locking-cache
INSTALL
IMPORT
SIG · LOCKING-CACHE
L
locking-cache
databasejavascriptv0.4.1
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.

lockingCache
✓ var lockingCache = require('locking-cache');
✗ import lockingCache from 'locking-cache';
Package is CJS only; ESM import may fail depending on bundler. Use require() or wrap with default interop.
lockingCache (factory)
✓ var locked = lockingCache(options);
✗ var locked = new lockingCache(options);
lockingCache is a function, not a constructor. Calling with 'new' will not cause an error but is unnecessary.
dispose callback
✓ lockingCache({ dispose: function(key, values) { ... } });
✗ lockingCache({ dispose: function(key, value) { ... } });
The dispose callback receives an array of values (to support varargs from unlock()), not a single value like in standard lru-cache.

Creates a locking cache and demonstrates deduplication: second fetch waits for first and receives cached result.

var lockingCache = require('locking-cache'); var request = require('request'); var lockedFetch = lockingCache({ max: 10 }); var fetch = lockedFetch(function(uri, lock) { var key = uri; return lock(key, function(unlock) { return request.get(uri, function(err, rsp, body) { return unlock(err, rsp, body); }); }); }); fetch('http://example.com/', function(err, rsp, body) { console.log(err, rsp.statusCode, body.length); }); fetch('http://example.com/', function(err, rsp, body) { console.log('Cached response:', rsp.statusCode, body.length); });
Debug
Known issues
gotchaThe dispose callback receives an array of values instead of a single value.
fix
Access values as an array, e.g., function(key, values) { console.log(values[0], values[1]); }.
affects: >=0.0.0
breakingPackage is CJS only; ESM imports may not work without a bundler interop.
fix
Use require() or configure bundler to handle CommonJS modules.
affects: *
deprecatedThe package has not been updated since 2016; relies on older lru-cache versions.
fix
Consider migrating to a more modern library like async-lru or p-limit with memoization.
affects: *
Errors
Common errors & fixes
TypeError: locked is not a function
Calling lockingCache without passing a factory callback or options object.
fix
Invoke lockingCache with an options object or a factory function: var locked = lockingCache({ max: 10 });
TypeError: lock is not a function
The user-provided function does not return a lock() call.
fix
Ensure the function passed to lockedFetch() returns the result of lock(key, cb) as shown in the usage.
Error: dispose is not a function
Passing dispose in options without wrapping values in array.
fix
The dispose callback receives an array of values: function(key, values) { ... }
Upgrade
Version history
0.4.1latest on npm
Audit
Dependencies
lru-cacherequiredProvides the underlying LRU cache storage and eviction policy.
Agent activity
12 hits · last 30 days
node
10
Resources
locking-cache — npm install locking-cache · libregistry