Skip to content

API for clearing cache #28

Closed
Closed

Description

@cherniavskii

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

Activity

  1. tannerlinsley commented on Nov 9, 2019

    @tannerlinsley
    Member
  2. tannerlinsley commented on Nov 11, 2019

    @tannerlinsley
    Member

    Released in version 0.3.7 🎉

  3. cherniavskii commented on Nov 12, 2019

    @cherniavskii
    ContributorAuthor

    @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

  4. tannerlinsley commented on Nov 12, 2019

    @tannerlinsley
    Member
  5. tannerlinsley commented on Nov 12, 2019

    @tannerlinsley
    Member

    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.

  6. cherniavskii commented on Nov 12, 2019

    @cherniavskii
    ContributorAuthor

    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?

  7. tannerlinsley commented on Nov 12, 2019

    @tannerlinsley
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions