TanStackT
TanStack•3y ago•
5 replies
exclusive-coral

cacheTime, maxAge and persistence

So my case is that for most of the queries I use, I want them to be persisted in storage forever. To achieve that, I set maxAge: Infinity in persister and cacheTime: Infinity in default query options, following the documentation. It all works.

However, there are some queries which I don't want to be persisted forever and I would like them to be garbage collected after, let's say, 10 minutes of inactivity. If I unsubscribe from such query and app is running for the next 10 minutes, it all works as expected. Query is removed from cache and it will not be persisted. But, if the app gets killed/closed after unsubscribing from the query, but before 10 minutes, the query will stay in the cache and it will be persisted. So when I open app again, after hydration query will be there with the cacheTime: Infinity from defaults, and it will basically stay in the cache forever if I never subscribe to it again.

Is there any way it can be solved? So if unsubscribe from this query, kill the app after 2 minutes and then open it again after an hour, the query won't get hydrated?

I'm afraid it's not because cacheTime property is not persisted in client state and there is no maxAge option on query level, so when hydration happens, the library has no idea if query was subscribed to or not, and how long it passed from unsubscribing in the moment of saving cache to the storage.

Are there any workarounds for that? šŸ¤”
Was this page helpful?