FusionCache icon indicating copy to clipboard operation
FusionCache copied to clipboard

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

Open neozhu opened this issue 1 year ago • 1 comments

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

neozhu avatar Oct 28 '23 13:10 neozhu

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.

AaronTorgerson avatar Dec 21 '23 21:12 AaronTorgerson

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

adospace avatar May 12 '24 20:05 adospace

Thanks a lot for your advice.

neozhu avatar May 13 '24 00:05 neozhu