Skip to content
Source
Sign inSign up
TanStack/query

API for clearing cache #28

Closedcherniavskii(GitHub profile, opens in a new tab) opened this issue 7 comments

The problem

There's a scenario, which is pretty common:

  • user logs in
  • some user data is fetched
  • user logs out
  • different user logs in and for a moment sees previous user's data

Normally, all user data should be deleted after log out, but there's no API for that at the moment.

I've tried setQueryData function to clear some queries, but it didn't work for me, since it doesn't make those query stale.

I've managed to clear cache like this:

import { queries } from 'react-query';

function handleLogout() {
    queries.length = 0;
}

But I'm not sure if it is safe to do this, since this is not a public API.

Proposed solution

Introduce clearQueries function, which will:

  • clear all queries if called without arguments
  • clear all queries except those provided as first argument.

The second option may be useful for applications, which are partly public. This will allow to clear user data cache, but keep public data cache.

Let me know do you think about that

  1. One way of getting around this is by using variables in your calls that key to that user.

    Tanner Linsley On Nov 9, 2019, 9:46 AM -0700, Andrew Cherniavskii <notifications@github.com>, wrote:

    The problem There's a scenario, which is pretty common:

    • user logs in • some user data is fetched • user logs out • different user logs in and for a moment sees previous user's data

    Normally, all user data should be deleted after log out, but there's no API for that at the moment. I've tried setQueryData function to clear some queries, but it didn't work for me, since it doesn't make those query stale. I've managed to clear cache like this: import { queries } from 'react-query';

    function handleLogout() { queries.length = 0; } But I'm not sure if it is safe to do this, since this is not a public API. Proposed solution Introduce clearQueries function, which will:

    • clear all queries if called without arguments • clear all queries except those provided as first argument.

    The second option may be useful for applications, which are partly public. This will allow to clear user data cache, but keep public data cache. Let me know do you think about that — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

  2. Released in version 0.3.7 🎉

  3. tannerlinsley closed this

  4. @tannerlinsley Thanks!

    I've checked it, and it looks like clearing state.data might not be enough. Consider case, when staleTime !== 0, and clearQueryCache is called. Then, new data won't be fetched until query is marked as stale.

    Looks like query state should be set to initial query state on clear:

    https://github.com/tannerlinsley/react-query/blob/b89576713282351a9b620decb50145fc7a426c82/src/index.js#L64-L73

    Also, stale timeout should be cleared as well.

    What do you think? I can submit a PR

  5. Yeah that would be great.

    Tanner Linsley On Nov 12, 2019, 3:13 AM -0700, Andrew Cherniavskii <notifications@github.com>, wrote:

    @tannerlinsley Thanks! I've checked it, and it looks like clearing state.data might not be enough. Consider case, when staleTime !== 0, and clearQueryCache is called. Then, new data won't be fetched until query is marked as stale. Looks like query state should be set to initial query state on clear: https://github.com/tannerlinsley/react-query/blob/b89576713282351a9b620decb50145fc7a426c82/src/index.js#L64-L73 Also, stale timeout should be cleared as well. What do you think? I can submit a PR — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

  6. Actually. Just delete all of the entries all the time. If a query is rerun and can’t find a query, it will get instantiated as needed. No need to be specific I think.

  7. tannerlinsley reopened this

  8. Just delete all of the entries all the time

    @tannerlinsley Do you mean delete all state entries for every query or delete all entries of queries array?

  9. Delete every query

    Tanner Linsley On Nov 12, 2019, 1:07 PM -0700, Andrew Cherniavskii <notifications@github.com>, wrote:

    Just delete all of the entries all the time

    @tannerlinsley Do you mean delete all state entries for every query or delete all entries of queries array? — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

  10. tannerlinsley closed this

Sign in to join this conversation.