ZiggyCreatures / FusionCache

FusionCache is an easy to use, fast and robust hybrid cache with advanced resiliency features.
MIT License
1.65k stars 87 forks source link

[Issue] How can I manually expire the cache in the following scenarios? #181

Closed neozhu closed 3 months ago

neozhu commented 10 months ago

Scenario 1: I have a paginated query for a product table. I need to cache the data whenever users switch pages. Each page switch results in caching of data, where the cache key is defined as key=$"{pageindex}{pagesize}{keywords}". This works as intended.

However, if I execute a delete or insert operation on the Product table, I need to refresh the related cached data for the Product. Here, the paginated query's data usually needs immediate refresh.

Furthermore, if a user has cached data based on a ProductId, and this specific ProductId data has just been modified (for instance, the corresponding product was deleted or updated), that cache should also expire.

How can I achieve manual cache refreshing or expiration in these cases? @jodydonetti

AaronTorgerson commented 8 months ago

This is asking a lot of a cache library @neozhu. You will need some sort of cache invalidation strategy that keeps track of the types and their associated "primary keys" stored under each cache key. This is probably better handled at a level "above" the cache itself. To do it properly, the cache library would somehow have to know which of the types in the graph of objects you place into a cache bucket can be updated and what their "identity" is (i.e. primary key). Like, what if the cache bucket contained an object of type Order which contained a reference to the updated Product? How would it know, then, what keys to evict? This is not an easy problem to solve and I would think it to be out of scope for a project like FusionCache.

Aside: One major issue to note is that nodes other than the one that performed the SQL update may have in-memory cache keys that it doesn't. This would thus require a pub/sub mechanism to notify the other nodes that, e.g., Product(id=123) was modified in order for them to look up which of their cache keys contained that entity.

adospace commented 3 months ago

You have to store in a list all the keys you generate. Every new key is added to the list. Then after any edit of your product table get that list and expire all the keys. Should work, hope it helps

neozhu commented 3 months ago

Thanks a lot for your advice.