alastairtree / LazyCache

An easy to use thread safe in-memory caching service with a simple developer friendly API for c#
https://nuget.org/packages/LazyCache
MIT License
1.72k stars 159 forks source link

Composite key support (or cache regions?) #86

Closed WhitWaldo closed 4 years ago

WhitWaldo commented 5 years ago

I've been using LazyCache quite happily for over a year now, so thank you for the time you've put into this package.

Today, I have a need for composite key support. I would like to be able to key cache entries by a user identifier and by certain argument values used in a given method. That's easily done by just hashing and concatenating the argument values.

However, I would also like to be able to invalidate based on a partial match of this composite key. For example, should a user's properties update, I would like to be able to invalidate the cached values for this user based on their user identifier even though I don't know the values of the remainder of the cached keys.

Your library doesn't seem to handle this out of the box and after a brief look through your implementation, it doesn't look like you have access to any of the keys stored in the cache via the IMemoryCache. I've considered implementing a dictionary to use as a non-authoritative source for the keys in the cache (not using this in a distributed sense today), but I can also see how this could introduce consistency issues if not handled carefully.

Do you have any other suggestions for going about implementing this? I'd be happy to do the work and eventually submit a pull request, but I'd appreciate your thoughts on it.

alastairtree commented 4 years ago

I think the best way to implement this would be to implement some kind of cache regions, where you get a seperate instance of CachingService (and the underlying cache provider) per region. Then to clear all keys in the region, you could just dispose/rebuild the cache service, which is the same as another often request feature - clear all items in the cache. You would just need a way to request your chosen cache region, probably by a string key.

Sound like something that would cover your use case?

nilmas commented 4 years ago

Consider the following if relevant in your context / scenario: https://stackoverflow.com/questions/8463962/using-multiple-instances-of-memorycache

Referring to above is the conclusion that when working in memory constrainted context the optimal scenario cannot be reached until Microsoft completes its work on cache regions.

alastairtree commented 4 years ago

This is a duplicate of #74 and I have added a work around to that card using cancellation tokens for a range of cache items over there, so closing this one.