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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove old cached items #4

Closed
JakeCoxon opened this issue Oct 28, 2019 · 6 comments
Closed

Remove old cached items #4

JakeCoxon opened this issue Oct 28, 2019 · 6 comments

Comments

@JakeCoxon
Copy link

JakeCoxon commented Oct 28, 2019

Hi great library. Is there any way to delete old cache items to avoid using up lots of memory? For example in an app with lots of pages where you want to cache if the user presses back but they are unlikely to go back more than 10. Perhaps this could be written in user space by using mutate(key, null) ?

@shuding
Copy link
Member

shuding commented Oct 29, 2019

Thanks @JakeCoxon that’s a good question! Yeah currently that’s the only way, because it might be dangerous to clear a cached key automatically: we don’t know if there’s any existing component still reading from that key.

To support that, we need to maintain a list for active keys (add/remove a key to the list when the SWR’s parent component is mounting/unmounted), which is not yet implemented.

Finally, we plan to export the cache APIs from the lib, and add options to the config soon. So you can override it with your own implementation like LRU cache, or just manipulate the cache object.

@hems
Copy link

hems commented Apr 27, 2020

Finally, we plan to export the cache APIs from the lib, and add options to the config soon. So you can override it with your own implementation like LRU cache, or just manipulate the cache object.

that sounds great!

@huozhi
Copy link
Member

huozhi commented Aug 28, 2021

SWR 1.0 has a new feature that can create custom cache provider to apply your own logic of managing cache. But as @shuding 's the comment above, this is still pretty dangerous to do which could lead to inconsistence between UI and cache state.

@huozhi huozhi closed this as completed Aug 28, 2021
@shuding
Copy link
Member

shuding commented Aug 28, 2021

I think a middleware and ref counting (#92) can work. I can build a prototype for this.

@shuding
Copy link
Member

shuding commented Aug 28, 2021

This middleware should work, maybe I can publish it as a package:

https://gist.github.com/shuding/915d6844db74e131ece951a2eff0f320

@piotr-cz
Copy link

piotr-cz commented Oct 5, 2021

@shuding The counter middleware removes key from cache, however the values corresponding to errorKey and isValidatingKey are still kept.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants