async-cache-dedupe
async-cache-dedupe copied to clipboard
implement "stale on error"
when an error occur, we can add the option "staleOnError" to serve the latest cached response, for example
const cache = createCache({ ttl: 60 })
cache.define('fetchUser', {
staleOnError: 10
},
(id) => database.find({ table: 'users', where: { id }}))
note the error is caused by the defined function, so for example here the database may not respond
for the first version, I'd go with a simple time (in seconds), then we could add a function to add some logic later, I'm not sure at the moment
we must also renew the cache ttl for staling entries
this logic should go here https://github.com/mcollina/async-cache-dedupe/blob/main/src/cache.js#L247
Came here to post a seemingly very similar feature request: a "stale-while-revalidate" style strategy that allows us to serve stale data until the request for latest data comes back. Happy to open a separate issue for this if the team feels that would be better.
A different issue would be better. A PR to implement it would be awesome too!
working on this - the stale-on-error one
done at https://github.com/mcollina/async-cache-dedupe/pull/45