Just wonder if there would be GetEnumerator() for MemoryCache, as supported in System.Runtime.Caching.Memorycache?
No current plans for it. The idea is that the moment after you enumerate - or even _while_ you enumerate, the cache may have purged some of the entries.
Can you share more about why you want this?
BTW we are looking at adding more diagnostic-related info to the cache system.
I am trying to build the cache like replica of the db table, so that it avoid frequent db hit in my project of device farm monitoring and management. And there's also one API which needs to read all the device info from the cache.
It's okay in my case to read the stale data even if it's polluted right after getAll(), as I just need a snapshot of the farm status at some time.
Have a look at https://github.com/aspnet/Caching/issues/129. Would that solve some of your needs?
Seems not really, as I was looking for using cache as replica of authoritative master and control when to write the cache to DB. So I need 'select *' from cache as from db table.
@ardpx If you have a single entry point where you insert data, you can populate a list of keys yourself, right? Making this general, is kind of dangerous for the issues Eilon outlined above.
This class is not supposed to provide GetEnumerator
and I also opened https://github.com/aspnet/Caching/issues/162 which is somehow related.
Do you mean you're storing the updated data in cache, and later sending the updates to the database? A problem you will have there is the cache can remove them before you commit the changes. If you're using the cache to reduce "read" operations that's a perfectly fine idea, but if you're looking to have background or batched "write" operations you should consider creating a class to queue up the work items which will be committed later. You can so that with a singleton service that holds a list of work item classes.
I would need the getenumerator too.
In my case I need to retrieve all items that starts with a particular string constant.
For the moment, I keep a list of keys added to the cache as suggested, but I think it's better to have enumerator and retrieve the Keys without using an external list
There are many times when I would love the be able to read (or delete) all of the cache entries based on a prefix. For example, I sometimes want to clear every cache entry containing a Product object because a user just changed our discount rates.
Ideally I would write .ClearByPrefix("Product: ")
.
Currently I just clear the whole cache, which of course removes unrelated items.
The ticket is bit old but still relevant in my case.
it would be nice to remove several keys from the IDistributedCache based on a regular expression. Enumeration is not necessary in my case (although it would be handy sometimes)
Is there any progress on this issue?
Most helpful comment
There are many times when I would love the be able to read (or delete) all of the cache entries based on a prefix. For example, I sometimes want to clear every cache entry containing a Product object because a user just changed our discount rates.
Ideally I would write
.ClearByPrefix("Product: ")
.Currently I just clear the whole cache, which of course removes unrelated items.