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

Selecting cache key starting with something? #66

Closed
dotnetshadow opened this issue Mar 29, 2019 · 4 comments
Closed

Selecting cache key starting with something? #66

dotnetshadow opened this issue Mar 29, 2019 · 4 comments
Labels

Comments

@dotnetshadow
Copy link

dotnetshadow commented Mar 29, 2019

Great library,

I was wondering is there a way to remove cache key based on a selector such as remove all keys starting with "product-id-" ?

As similar question was asked here:
#6

Looking for something like below, but for .net core

 {
            var cacheKeys = _cachingService.ObjectCache.Where(kvp => kvp.Key.Equals("MyCacheKey"))
                .Select(kvp => kvp.Key);
            foreach (var cacheKey in cacheKeys)
            {
                _cachingService.ObjectCache.Remove(cacheKey);
            }
        }

Also is the a feature where a cache object can be dependent on another cache object.
i.e. Cache A has a dependency on Cache B (when it's cleared), I would like cache a to be cleared too

@alastairtree
Copy link
Owner

alastairtree commented Mar 29, 2019

The simple answer is that key iteration is blocked on the new version of MemoryCache, the underlying Microsoft library LazyCache builds on top of, so LazyCache cannot easily give you a built-in way to solve it. Blocking iteration on all the keys is sensible for performance and concurrency reasons. This is a breaking change in the upgrade from 0.7 to 2.0.

To solve your requirement I can think of two ways:

  1. Use a separate instance of CachingService everywhere you need to use keys product-id- and have this use a separate provider which you can then dispose and recreate to clear the "product cache" separate from the main cache:
IAppCache productCache = new CachingService(new MemoryCacheProvider(new MemoryCache(new MemoryCacheOptions())));

// do some caching

// and then later on when you want to clear it - dispose and recreate
productCache.CacheProvider.Dispose();
productCache = new CachingService(new MemoryCacheProvider(new MemoryCache(new MemoryCacheOptions())));
  1. Alternatively you could cache the relationship between the keys (and so have a type of cache regions feature), using a cached list of keys to allow you retrieve all dependent cache keys. This stack overflow post has a good example. You might then want to wrap the logic to add something to a cache dependency list, or to remove all inside that list, in extension method to make it more re-usable.

Does this allow you to work around the change?

@dotnetshadow
Copy link
Author

@alastairtree Cheers thanks for that, I actually ended up going for the second version which does the job for now. I like the suggestion to have a wrapper with extension methods.

Maybe if other people require this functionality it might be worth looking into making it a feature for your library

@agrath
Copy link

agrath commented Sep 4, 2019

#86 Has a pull request maintaining an internal cache key list

@alastairtree
Copy link
Owner

This is a very similar to #74 and I have added a workaround to clearing a range of items using cancellation tokens as a comment there which may be useful to folks who find this ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants