Cache the function result by wrapping it
npm i cached-call
const CachedCall = require('cached-call')
const cache = new CachedCall()
cache({ someHeavyJob (v) { return v }, maxAge: 1000 })(v)
const cache = new CachedCall()
cache({ [name]: func, maxAge, max, stale, key })(...args)
name | description | default | example |
---|---|---|---|
name |
Function name for cache key | (required) | |
func |
Cache target function | (required) | |
key |
Key for create cache key (function or value) | (...args) => args |
1 , 'moo' |
maxAge |
Maximum age in ms | (required) | 300 , 100 |
args |
Arguments that will be passed to key function |
(required) | |
max |
The maximum size of the cache | Infinity |
1000 , 100 |
stale |
return the stale value before deleting it | false |
true |
cacheError |
Cache error and rejection for ms | undefined |
1000 |
JSON.stringify([name, ...args])
const key = (nan, fn, n1, n2) => [n1, n2]
const pickArgs = cache({ someFunction, key, maxAge: 100 })
pickArgs(NaN, () => {}, 1, 2) // cache key : ["someFunction", 1 ,2]
const dayjs = require('dayjs')
const everyHour = min => () => dayjs().add(60 - min, 'm').startOf('m').minute(min) - Date.now()
const every10minutes = cache({ someFunction, maxAge: everyHour(10) })
const every20minutes = cache({ someFunction, maxAge: everyHour(20) })
const retryThrottle = cache({ someFunction, maxAge: 30000, cacheError: 1000 })
retryThrottle()
The MIT License (MIT)
Copyright (c) 2020 Elevista