async-cache-dedupe icon indicating copy to clipboard operation
async-cache-dedupe copied to clipboard

implement "stale on error"

Open simone-sanfratello opened this issue 3 years ago • 3 comments

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

simone-sanfratello avatar Jan 17 '22 07:01 simone-sanfratello

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.

lewisl9029 avatar Jul 25 '22 13:07 lewisl9029

A different issue would be better. A PR to implement it would be awesome too!

mcollina avatar Jul 25 '22 14:07 mcollina

working on this - the stale-on-error one

simone-sanfratello avatar Jul 27 '22 06:07 simone-sanfratello

done at https://github.com/mcollina/async-cache-dedupe/pull/45

simone-sanfratello avatar Jan 18 '23 10:01 simone-sanfratello