Decorator to memoize the results of async functions via lru-cache.
- Uses lru-cache for caching impl
- Uses object-hash for implicit cache key derivation from function args
- Thoroughly tested in production
npm install --save p-cache
const request = require('request-promise-native')
const pCache = require('p-cache')({
label: 'test-service',
// pass args to lru-cache here
max: 1000
})
// returns an async function which gets the given url, with the results being
// cached via lru-cache up to 1000 urls
module.exports = pCache(async (url) => {
return request(url)
})
Returns a function decorator that will wrap calls to the target function in an lru-cache instance defined by these opts
.
opts
- object, optionalopts.label
- string, optional label to use for displaying cache hits and missesopts.log
- function, optional function to use for displaying cache hits and misses (default:console.log
)
Note all unrecognized options are passed onto lru-cache
opts.max
- number, optional passed onto lru-cacheopts.maxAge
- number, optional passed onto lru-cacheopts.length
- function, optional passed onto lru-cacheopts.dispose
- function, optional passed onto lru-cacheopts.stale
- boolean, optional passed onto lru-cacheopts.noDisposeOnSet
- boolean, optional passed onto lru-cache
pCache(opts) => Function<Promise>(async function(...args))
MIT © Travis Fischer
Support my OSS work by following me on twitter