-
-
Notifications
You must be signed in to change notification settings - Fork 341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature request] some kind of "return stale value, but keep updating" signal #268
Comments
Hmm, Maybe to clarify, there could be two options, both of which could be mutable by the fetchMethod:
So, to signal back up from the fetchMethod to the cache, that you expect this fetch to take some specific amount of time, you could do: const cache = new LRUCache<string, APIResponse>({
fetchMethod: async (key, oldVal, { options, signal }) => {
// calculate the expected time to wait somehow
options.fetchTimeout = calculatedExpectedWaitTime()
return slowBackendAPIRequest(key, signal)
}
} This also doesn't require that the consumer on either end of the cache has to deal with custom events or other weird abuses of the AbortController/AbortSignal interface. |
Oh, there is a native interface for |
With the ability to pass in an AbortSignal, you'd be able to do this, if there was a "ignoreFetchAbort" option: const c = new LRUCache({
fetchMethod: async (key, oldValue, { signal }) => {
return await longSlowThing(key)
},
allowStaleOnFetchRejection: true,
ignoreFetchAbort: true,
})
// will definitely return *something* in 100ms,
// either the cached value or `undefined`,
// but if a value eventually resolves, it'll be cached
const value = c.fetch('key', { signal: AbortSignal.timeout(100) }) |
(inspired by thinking through #263)
It'd be nice to be able to use
fetch()
to say something like: "Try to get the new value from the source, but if it takes longer than${time}
, just return the stale value, and continue updating in the background".One way to maybe do that is to use the abort signal as a method for signaling an early return? Then the fetchMethod could do something like:
That could be sugared up into a
maxFetchTime
option, so like:What do you think? Would you use this if it existed?
The text was updated successfully, but these errors were encountered: