Is there anything available at this time for getting list of keys?
Using Following workaround at the moment. I am using .net core 3.1
private static List<string> GetAllKeysList()
{
var field = typeof(MemoryCache).GetProperty("EntriesCollection", BindingFlags.NonPublic | BindingFlags.Instance);
var collection = field.GetValue(Cache) as ICollection;
var items = new List<string>();
if (collection != null)
foreach (var item in collection)
{
var methodInfo = item.GetType().GetProperty("Key");
var val = methodInfo.GetValue(item);
items.Add(val.ToString());
}
return items;
}
If you really, really, really have to do something like this, at least move the var methodInfo = item.GetType().GetProperty("Key"); part out of the foreach loop to avoid a double reflection lookup per each item.
Btw maybe this is what you are looking for.
I don't answer If you really, _really_, really have to do something like this, at least move the
var methodInfo = item.GetType().GetProperty("Key");part out of the foreach loop to avoid a double reflection lookup per each item.Btw maybe this is what you are looking for.
I know about option in System.Runtime.Cache. where i get the list of all keys like
var cacheKeys = MemoryCache.Default.Select(kvp => kvp.Key).ToList();
Here i am looking for the list of all keys in Microsoft.Extensions.Caching.Memory
I have got the above option from
https://stackoverflow.com/questions/45597057/how-to-retrieve-a-list-of-memory-cache-keys-in-asp-net-core
This isn't something that's currently supported, it would require an API change to the IMemoryCache interface. What's the scenario where you find you need to list cache keys? In general, we don't want to force all caching implementations to provide a way to list keys since it can be costly to implement and leaks some of the abstraction we're trying to provide.
This isn't something that's currently supported, it would require an API change to the
IMemoryCacheinterface. What's the scenario where you find you need to list cache keys? In general, we don't want to force all caching implementations to provide a way to list keys since it can be costly to implement and leaks some of the abstraction we're trying to provide.
i have an old implementation of object caching which i have migrated in .net core.
In some methods i am in listing all keys then removing the keys which are starts with certain input string.
secondly i am clearing all caching keys on some specific condition.
I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label.
+1, .net has two(!) implementations of MemoryCache and both are terrible!
Thanks @KamranShahid for your issue. The next step for this would be to add API proposal, and example usage. Refer to https://github.com/dotnet/runtime/blob/master/docs/project/api-review-process.md for more detail.
If you really, _really_, really have to do something like this, at least move the
var methodInfo = item.GetType().GetProperty("Key");part out of the foreach loop to avoid a double reflection lookup per each item.Btw maybe this is what you are looking for.
Can you suggest how?