My team and I are currently maintaining a caching system that is purely based on keys. From what I've read we are striving for a system that's based on the cache-aside strategy. Where we have a website that talks to a rest service using httpclients. These clients make use of a memory cache which works based on a combined key. When another method is called that would invalidate a cache item, we havez to manually instruct the memorycache to remove it.
Let me explain by example.
MemoryCacheStore
=> object stored in a singleton for the entire webapplication (Asp.Net MVC)
Pseudo code explaining the case:
public class CustomerRestClient {
private readonly IMemoryCacheStore _cacheStore;
private readonly User user;
public CustomerRestClient(IMemoryCacheStore cacheStore, User user){
_cacheStore = cacheStore;
_user = user;
}
public Customer Get(int id){
return _cacheStore.Get($"Customer-Get-{id}-{_user.Id}", TimeSpan.FromMinute(10), () => RestService.Get(id));
}
public Customer Update(Customer customer){
var newCustomer = RestService.Update(customer);
_cacheStore.InvalidateKey($"Customer-Get-{customer.id}-{_user.Id});
return newCustomer;
}
}
The problem with this approach is that the developer needs to know which keys to invalidate whenever an object has changed. We are considering trying to solve this with some kind of cachekeymanager to eliminate the key string magic but imho it doesn't solve the problem that the developer still needs to know what to invalidate when and these definitions are spread out throughout all of our restclients.
You might argue that this shouldn't be a problem and I must admit in the beginning this wasn't. But as the codebase grows larger, objects change name, this is becoming a management nightmare with several bug reports as the result.
So my question is;
Do you know of any caching strategies or frameworks I can look into which may resolve our case?
I'm thinking more in the direction of defining a dependency graph and I'm hoping someone has already tackled this problem which may give us new insights.