You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Getting all cache keys Microsoft.Extensions.Caching.Memory #36026
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.
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.
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
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 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.
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.
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.
@KamranShahid As @maryamariyan has pointed (#36026 (comment)), you need to provide the API proposal using the official template. Once you do that and we have a better understanding of what problem the API would try to solve, we can continue the process of adding new API.
@KamranShahid As @maryamariyan has pointed (#36026 (comment)), you need to provide the API proposal using the official template. Once you do that and we have a better understanding of what problem the API would try to solve, we can continue the process of adding new API.
requirement is quite clear and i have put a solution as well which i am using
Activity
KamranShahid commentedon May 6, 2020
Using Following workaround at the moment. I am using .net core 3.1
jodydonetti commentedon May 6, 2020
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.
KamranShahid commentedon May 6, 2020
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
analogrelay commentedon May 6, 2020
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.KamranShahid commentedon May 7, 2020
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.
Dotnet-GitSync-Bot commentedon May 7, 2020
I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label.
specialforest commentedon Aug 4, 2020
+1, .net has two(!) implementations of MemoryCache and both are terrible!
maryamariyan commentedon Oct 7, 2020
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.
KamranShahid commentedon Oct 14, 2020
Can you suggest how?
sigmarsson commentedon Mar 25, 2021
I too display interest in dealing somehow with the keys.
My key is like
$"{object}+{DateTime}-{DateTime}"Therefore my cache legitim could furnish the cached value for the following key request :
$"{object}+{DateTime + 1 Day}-{DateTime - 1 Day}"because this period is contained by the previous one.I am not sure anymore this class is the right one here. What is your take on ?
adamsitnik commentedon Mar 26, 2021
@KamranShahid As @maryamariyan has pointed (#36026 (comment)), you need to provide the API proposal using the official template. Once you do that and we have a better understanding of what problem the API would try to solve, we can continue the process of adding new API.
KamranShahid commentedon Apr 14, 2021
requirement is quite clear and i have put a solution as well which i am using
maryamariyan commentedon Apr 23, 2021
I don't see us working on this feature item anytime soon, closing since it does not seem like a blocking item either.