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

InvalidCache with wildcards #2

Open
FKSI opened this issue Feb 6, 2024 · 3 comments
Open

InvalidCache with wildcards #2

FKSI opened this issue Feb 6, 2024 · 3 comments

Comments

@FKSI
Copy link

FKSI commented Feb 6, 2024

If an user goes to /books and perform a query to /books?query="harry potter" both are cached. If user add to its favorites the book from the query. Then we need to invalidate both routes.

Since /books displays the favorites at the top.

It would be great to be able to invalidate the cache from a wildcard such as invalidateCache("/books*")

Great job btw!

@AlemTuzlak
Copy link
Contributor

@FKSI Thank you! Will try to add this in the near future

@FKSI
Copy link
Author

FKSI commented Feb 15, 2024

In the meantime I found a way to make a wildcard system:

  • Use localStorage as cache
  • Using default key with current route
  • Upon invalidation loop over the keys and returns the ones that matches your needs
  • Invalidate (or actually remove them from localStorage).

@HazelDaniel
Copy link

Another trick is:

  • use indexeddb as the storage for the cache adapter
  • use a namespace system for the keys when setting them like (pathname_of_the_current_route_url:the_pattern_of_the_current_route)
  • Add an index on the name space for performance boost.
  • on the setItem method, parse the key into the original key (pathname_of_the_current_route_url) and namespace (the_pattern_of_the_current_route)
  • upon invalidation, use the index to retrieve the elements and delete each of the corresponding entries

The idea here is, whenever you are trying to invalidate a group of dynamic routes, just
use an index on the pattern as there might be a bunch of keys to go through

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

3 participants