Persistence, cacheTime and garbage collector
Hi! I'm using
In above case, when
persistQueryClient plugin and I set up default cacheTime on queryClient to Infinity, as described here in the docs:
https://tanstack.com/query/latest/docs/react/plugins/persistQueryClient#how-it-works
So garbage collection is basically disabled for all queries by default which is what I expect most of the time.
However, there are queries which I would like to remove from the cache right after they become inactive. Here is a snippet using some example data:
In above case, when
someFilters variable changes, I would like to fetch data for new filters and I would like to remove data for previously selected filters immediately. I will never need it again and don't want it to be persisted.
So I only care about results for currently selected filters.
I tried to set cacheTime: 0 in above case. I thought that it will remove data for previous key after filters change, and data for current filters will be persisted because query is active (this custom hook is mounted at the root).
Although it doesn't always work. Sometimes after someFilters changes I see two versions of this query in devtools - one for active query with cacheTime being properly set to 0 (which is expected) and the other, inactive one, for previous filters with cacheTime being set to Infinity, which I guess is inherited from query client defaults. So in such scenario query for old filters configuration will be persisted forever.
Is it expected behaviour or is it sth broken with my code? The way cacheTime works when queries get unmounted and how it works during hydration is not super clear to me unfortunately 🤔1 Reply
tame-yellowOP•3y ago
Ok I got it:
I was prefetching this data in other place using
queryClient.fetchQuery. I didn't set cacheTime there so I guess it was inherited from defaults (Inifinity) and it was picked because bigger cacheTime always win