Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

clear missing on cache interface #2117

Closed
swushi opened this issue Aug 12, 2022 · 1 comment
Closed

clear missing on cache interface #2117

swushi opened this issue Aug 12, 2022 · 1 comment

Comments

@swushi
Copy link

swushi commented Aug 12, 2022

Hi! 馃憢

In the documentation it is specified that you can clear the cache here using:

cache.clear();

However, clear is not included in the Cache interface. This is a patch I have been using.

Here is the diff that solved my problem:

diff --git a/node_modules/swr/dist/types.d.ts b/node_modules/swr/dist/types.d.ts
index d97de4e..f50cdc8 100644
--- a/node_modules/swr/dist/types.d.ts
+++ b/node_modules/swr/dist/types.d.ts
@@ -104,5 +104,6 @@ export interface Cache<Data = any> {
     get(key: Key): Data | null | undefined;
     set(key: Key, value: Data): void;
     delete(key: Key): void;
+    clear(): void;
 }
 export {};

This issue body was partially generated by patch-package.

@huozhi
Copy link
Member

huozhi commented Aug 13, 2022

We released a new API change latest beta v2.0.0-beta.5 that could let you do the following code to remove all cache data much easier:

// declaration 
// mutate(keyMatcher, data, revalidate)

mutate(/* key matcher */ (key) => true, undefined, false)

Notice that the functional key is replaced by a key filter function. That's the better way than cache.clear because it could keep the state sync with the actual cache provider. We don't recommend to manipulate the cache directly in the codebase.

The v2 docs are still in progress, the more details about the mutate API change are in PR #1989 or RFC #1946

The idea was to leverage the existing mutate API but extend the ability to partially mutat selected keys. That example calls means: Use () => true as a filter is to match all the existing keys and mutate them to undefined, without revalidation.

We'll give more examples about that change in v2 docs later.

@swushi swushi closed this as completed Aug 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants