Decorator to memoize the results of async functions via lru-cache.
Decorator to memoize the results of async functions via lru-cache.
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-cache
opts.maxAge
- number, optional passed onto lru-cache
opts.length
- function, optional passed onto lru-cache
opts.dispose
- function, optional passed onto lru-cache
opts.stale
- boolean, optional passed onto lru-cache
opts.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