2.0.0 • Published 2 years ago
@fabsrc/re-mem v2.0.0
re-mem
Fork of mem with "staleWhileRevalidate" and "staleIfError"
Install
npm install -S @fabsrc/re-memUsage
import reMem from "@fabsrc/re-mem"
function getData(id) {
return Promise.resolve(`Data: ${id}`)
}
const getDataMemoized = reMem(getData, {
maxAge: 1000,
staleWhileRevalidate: 10000,
staleIfError: 20000
})
getDataMemoized(123)
.then(console.log)
.catch(console.error)Options
cacheKeyFunction that returns a cache key based on arguments passed to a function. By default the first argument is used as cache keycacheCustom cache to store data in. (Default:new Map())cachePromiseRejectionBoolean flag wether to cache rejected Promises or not (Default:false)maxAgeTime in ms to return the cached promise (Default:Infinity)staleWhileRevalidateTime in ms to return stale data while revalidating the data in the background. The time starts aftermaxAgeruns out.staleIfErrorTime in ms to return stale data if original promise rejects with an error.
Development
Testing
npm test