0

In useInfiniteQuery, If we make a cacheTime 0 will it make the cache inactive ?

I see it goes to stale state by default but it is not garbage collected. As per the document I understand that based on the cache time the garbage collection happens. Can you some one explain

2
  • It will be garbage collected once the query becomes unused/inactive. Commented Nov 28, 2022 at 17:24
  • What would be the time frame for garbage collection ? In documentation it is mentioned as 5 mins(as cache is 5 mins). In my case the cacheTime is 0 and the query is in stale state but still it is not garbage collected Commented Nov 30, 2022 at 4:01

1 Answer 1

1

I think you're misunderstanding what a stale and inactive query means.

staleTime is the time in milliseconds that determines for how long the query is considered as "fresh". This means that as long as the query is marked as fresh, no network requests will be made after the first successful one and the data will always be read from the cache when needed. If the query is marked as "stale", then a background refetch can happen under certain conditions such as window focus (if enabled) for example.

A query is considered inactive when there are no observers registered. In more simple words, when all the components that use the query are unmounted.

A query's cached data can only be garbage-collected when it's marked as inactive for as long as the established cacheTime.

From what you explain, you configured the cacheTime to be 0. This means that as soon as all the components that use the query are unmounted, the cache for that query will be garbage-collected. If this is not happening, it's because one or more components that use this query remain mounted (the query is still active).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.