What is the purpose of having `cacheTime` set to a higher value than `staleTime`?
When configuring options for a query (or globally), the
cacheTime
value controls how long data is kept in the in-memory cache before being garbage collected. The staleTime
value controls how long cached data is considered fresh. Once this time expires, any requests will data will trigger a new fetch. By default, cacheTime
is set to 5 minutes, and staleTime
is set to 0 (meaning cached data is never fresh).
It seems like this configuration (or any configuration where cacheTime > staleTime
) is kind of pointless. Why would you keep data persisted in the cache that you know is stale? Is there some scenario where having known-stale data provides a benefit over having the cache entry for that value be null?
Conversely, it also seems pointless to have cacheTime < staleTime
, since in that scenario would result in cache misses for "fresh" data that had been garbage collected.
It seems like these values should always be equal to each other.4 Replies
fair-rose•3y ago
when data is "stale" but not garbage collected, mounting a query will
1. trigger a refetch
2. immediately return the stale data
3. when the refetch completes, rerender with the fresh data
conscious-sapphireOP•3y ago
ah ok .... so if the cache is empty, no data will be displayed/available until after the fetch completes. Whereas if its stale, there will still be data returned immediately.
fair-rose•3y ago
exactly, then it's just a matter of "preference" or "business requirements" whether you're ok with temporarily showing some data that might be a little out of date (I guess it depends on how much of the data you're showing on your UI, how granular the query is, and how much / how frequently it gets mutated)
conscious-sapphireOP•3y ago
cool, thanks