Registry / database / dataloader-cache-lru

dataloader-cache-lru

JSON →
library1.0.1jsnpmunverified

An LRU (least recently used) cache implementation compatible with Facebook's DataLoader. Version 1.0.1 is the current stable release; the package has not seen updates since 2017, reflecting a maintenance or deprecated state. It wraps the lru-cache module to provide bounded memory usage for DataLoader instances that persist across requests, such as in GraphQL contexts. Unlike the default Map cache, it evicts least recently used entries when the cache size limit is exceeded. Also supports clearing specific keys when records mutate. This package is specific to DataLoader and offers no alternative caching strategies.

npm install dataloader-cache-lru
INSTALL
IMPORT
SIG · DATALOADER-CACHE-L
D
dataloader-cache-lru
databasejavascriptv1.0.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.

default (function)
✓ import createCache from 'dataloader-cache-lru';
✗ import { createCache } from 'dataloader-cache-lru';
The module exports a single function as default. Named export is incorrect.
default (require)
✓ const createCache = require('dataloader-cache-lru');
✗ const { createCache } = require('dataloader-cache-lru');
CommonJS require returns the function directly; destructuring is wrong.
TypeScript
✓ import createCache from 'dataloader-cache-lru';
✗ import * as createCache from 'dataloader-cache-lru';
TypeScript users should use default import; namespace import is not needed and may cause type issues.

Shows creation of an LRU cache with max size 3, using it with DataLoader, loading a key, and clearing it.

const DataLoader = require('dataloader'); const createCache = require('dataloader-cache-lru'); const cacheMap = createCache({ max: 3 }); const batchLoadFn = (keys) => Promise.all(keys.map(k => ({ key: k, data: k }))); const loader = new DataLoader(batchLoadFn, { cacheMap }); loader.load('a').then(console.log); console.log(cacheMap.keys().sort()); // ['a'] loader.clear('a'); console.log(cacheMap.keys().sort()); // []
Debug
Known issues
breakingThe package has no TypeScript definitions.
fix
Use @types/dataloader-cache-lru if available, or declare module manually.
affects: *
deprecatedPackage not updated since 2017; lru-cache versions may have breaking changes.
fix
Consider alternatives like @keyv/lru or manual LRU using lru-cache directly.
affects: >=1.0.0
gotchaThe cache object's methods (set, get, delete, clear, keys) must match DataLoader's expected interface.
fix
If using custom cache, ensure it implements the CacheMap interface.
affects: *
gotchaOptions passed to createCache are passed directly to lru-cache constructor.
fix
Refer to lru-cache documentation for options like max, maxAge, etc.
affects: *
Errors
Common errors & fixes
TypeError: createCache is not a function
Importing a named export instead of default
fix
Use `const createCache = require('dataloader-cache-lru');` or `import createCache from 'dataloader-cache-lru';`
DataLoader requires a valid cache map (cacheMap must have a function 'get')
Passing result of createCache without calling it as a function
fix
Call createCache: `const cacheMap = createCache({ max: 100 });`
Cannot find module 'lru-cache'
Missing peer dependency lru-cache
fix
Run `npm install lru-cache` as this package depends on it.
The provided cache does not support 'delete' method. DataLoader requires delete.
Using a plain Map() or object that doesn't implement delete
fix
Use the cache object returned by createCache, which implements delete.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies
lru-cacherequiredUnderlying LRU cache implementation
Agent activity
9 hits · last 30 days
node
8
Resources
dataloader-cache-lru — npm install dataloader-cache-lru · libregistry