mcollina / async-cache-dedupe

Async cache with dedupe support
MIT License
640 stars 40 forks source link

Stale-while-revalidate style cache strategy #33

Closed lewisl9029 closed 1 year ago

lewisl9029 commented 2 years ago

For some use cases where we don't necessarily need the latest data as soon as ttl is passed, it can be useful to be able to specify a separate interval for staleness so we can continue serving data that's considered stale while we trigger a refetch in the background.

This feature is similar in principle to #14, and can possibly share some implementation details.

Proposal:

Accept a new staleWhileRevalidate: number argument (or maybe staleWhileRefetch? since we're not necessarily going to be doing any revalidation here), specifying the staleness interval.

Between the ttl and the staleness interval, data is considered stale, and any requests for it will resolve immediately with stale data while triggering a deduped refetch of the data in the background. After the interval, stale data must not be used and requests for the data must await on the refetch of the new data before being served.

Defaulting to 0 should preserve existing behavior where no stale data is ever served.

Thoughts?

I have a bunch of higher priority customer-facing stuff I have to work on over the next few weeks before I'm going to be able to work on an optimization making use of this, but I'd be happy to take a crack at a PR if nobody has tackled it by then!

mcollina commented 2 years ago

Go for it, it would be amazing!

simone-sanfratello commented 1 year ago

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