Registry / storage / lru-cache-promise

lru-cache-promise

JSON →
library1.0.9jsnpmunverified

lru-cache-promise (v1.0.9) is a thin wrapper around Isaac Z. Schlueter's lru-cache that adds promise-based async value fetching via a getAsync method. It uses Bluebird for promises and ensures that only one fetch request per key is in flight at a time, deduplicating concurrent calls. The package is stable with no recent releases and minimal maintenance. It differs from other caching libraries by being extremely lightweight and focused solely on adding promise support to lru-cache, without additional features like TTL or statistics.

npm install lru-cache-promise
INSTALL
IMPORT
SIG · LRU-CACHE-PROMISE
L
lru-cache-promise
storagejavascriptv1.0.9
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
import createCache from 'lru-cache-promise';
const { default } = require('lru-cache-promise');
The default export is a factory function that creates a cache instance. Import via default import syntax. In CommonJS, use require('lru-cache-promise')() – calling the result.
cache instance
const cache = require('lru-cache-promise')();
const cache = require('lru-cache-promise');
The module exports a factory function, not a pre-created cache instance. Always call the require result as a function.
getAsync
cache.getAsync('key', fetchFn).then(console.log);
cache.get('key');
getAsync returns a Bluebird promise. The synchronous get() from the underlying lru-cache is also available but returns the value directly (or undefined).

Shows how to use getAsync with a fetch function that returns a promise, including deduplication of concurrent requests.

import createCache from 'lru-cache-promise'; const cache = createCache({ max: 100 }); function fetchUser(id) { // Simulate async fetch return new Promise((resolve) => { setTimeout(() => resolve({ id, name: `User ${id}` }), 100); }); } cache.getAsync('user:1', fetchUser) .then(user => console.log(user)) .catch(err => console.error(err)); // Deduplication: multiple concurrent calls for same key Promise.all([ cache.getAsync('user:1', fetchUser), cache.getAsync('user:1', fetchUser) ]).then(([u1, u2]) => { console.log('Both resolve with same object:', u1 === u2); // true });
Debug
Known issues
gotchaThe factory function requires instantiation. Forgetting to call the function will result in an error like 'TypeError: cache.getAsync is not a function'.
fix
Call the require result as a function: require('lru-cache-promise')()
affects: all
deprecatedDepends on bluebird; not using native Promises. This may complicate debugging or Promise chain interop with native Promises.
fix
Use a library that returns native Promises or wrap the result in Promise.resolve() if needed.
affects: all
gotchaUnderlying lru-cache version 2.x uses deprecated Node.js APIs and may emit deprecation warnings in Node 12+.
fix
Consider upgrading to a cache library that supports lru-cache v6+, e.g., lru-cache-fp or lru-cache-async.
affects: >=4.0
breakingBluebird's Promise implementation is used; do not assume native Promises are used. .finally() may require Bluebird's version.
fix
Use .lastly() from Bluebird or convert with Promise.resolve(cache.getAsync(...)).
affects: all
Errors
Common errors & fixes
TypeError: cache.getAsync is not a function
The module exports a factory function. You forgot to call it.
fix
const cache = require('lru-cache-promise')();
ReferenceError: Promise is not defined
Bluebird is not loaded or the environment lacks a Promise implementation.
fix
Ensure bluebird is installed: npm install bluebird
TypeError: fetchFunction is not a function
getAsync expects a function as second argument.
fix
Pass a function that returns a value or a promise.
Upgrade
Version history
1.0.9latest on npm
Audit
Dependencies
lru-cacherequiredCore caching implementation; lru-cache-promise wraps lru-cache's synchronous API.
bluebirdrequiredPromise library used for getAsync; Bluebird's Promise is required for the promise behavior.
Agent activity
16 hits · last 30 days
node
14
OpenAI (training)
1
Resources