How to clear cached data (security standpoint)
We have a multi-stage data loading partially subscriptions from the server.
A typical flow
- 1) Get the uid from out auth library ex. "someUid"
- 2) Based on the auth we subscribe for the group ids the user is added to ex. ["aaaAAA", "bbbBBB", "cccCCC"]
- 3) Based on each of the group ids we subscribe for group info. in our case it's 3 separate subscriptions. xx. "aaaAAA" -> { "name": "A team"}, bbbBBB -> { "name": "Betas" }, cccCCC -> { "name": "Crips" }
- We get un update in subscription no.2 - the user was removed from one of the groups the current groups are ["aaaAAA", "cccCCC"].
What is the best way to to clear the cached group info for the group with id cccCCC - the one that the user was removed from. It's not a matter of performance, but about limiting the information kept on the device from the data sec standpoint

4 Replies
ambitious-aqua•3y ago
queryClient.removeQueries() maybe?
https://tanstack.com/query/latest/docs/react/reference/QueryClient#queryclientremovequeriesQueryClient | TanStack Query Docs
QueryClient
The QueryClient can be used to interact with a cache:
adverse-sapphireOP•3y ago
Definitely removeQueries can help, but removing queries has to happen according to the previously cached data and the new data. On our example we had groups:
["aaaAAA", "bbbBBB", "cccCCC"] (cached)
And now we have
["aaaAAA", "cccCCC"]
So to find out that subcription for "bbbBBB" group data we need to compare the cached and the new data. We can write it "manually", by explicitly requesting it from cache. My question is if there is an idiomatic way of handling this?
ambitious-aqua•3y ago
Not sure it's the best way but I would probably try setting up a QueryObserver on the group IDs query. Then every time the observer receives data, call removeQueries() with a
predicate function to remove all the queries that are not in the group IDs query anymore.
See predicate on QueryFilters: https://tanstack.com/query/latest/docs/react/guides/filters#query-filters
QueryObserver: https://tanstack.com/query/latest/docs/react/reference/QueryObserverQueryObserver | TanStack Query Docs
QueryObserver
The QueryObserver can be used to observe and switch between queries.
Filters | TanStack Query Docs
Some methods within TanStack Query accept a QueryFilters or MutationFilters object.
Query Filters
ambitious-aqua•3y ago
I don't think there is a more "automated" way.