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

Getting all cache keys Microsoft.Extensions.Caching.Memory #36026

Closed
KamranShahid opened this issue May 6, 2020 · 14 comments
Closed

Getting all cache keys Microsoft.Extensions.Caching.Memory #36026

KamranShahid opened this issue May 6, 2020 · 14 comments
Labels
area-Extensions-Caching feature-request needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration
Milestone

Comments

@KamranShahid
Copy link

Is there anything available at this time for getting list of keys?

@KamranShahid
Copy link
Author

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;
        }

@jodydonetti
Copy link
Contributor

jodydonetti commented 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
Copy link
Author

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

@analogrelay
Copy link
Contributor

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.

@KamranShahid
Copy link
Author

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.

@analogrelay analogrelay transferred this issue from dotnet/extensions May 7, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label May 7, 2020
@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label.

@analogrelay analogrelay added this to the Future milestone May 7, 2020
@maryamariyan maryamariyan removed the untriaged New issue has not been triaged by the area owner label Jul 1, 2020
@specialforest
Copy link

+1, .net has two(!) implementations of MemoryCache and both are terrible!

@maryamariyan
Copy link
Member

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
Copy link
Author

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?

@sigmarsson
Copy link

sigmarsson commented 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
Copy link
Member

@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.

@ghost
Copy link

ghost commented Apr 9, 2021

This issue has been automatically marked no recent activity because it has been marked as needs author feedback but has not had any activity for 14 days. It will be closed if no further activity occurs within 7 more days. Any new comment (by anyone, not necessarily the author) will remove no recent activity

@KamranShahid
Copy link
Author

@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

@ghost ghost added needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration and removed needs author feedback no-recent-activity labels Apr 14, 2021
@maryamariyan
Copy link
Member

I don't see us working on this feature item anytime soon, closing since it does not seem like a blocking item either.

@ghost ghost locked as resolved and limited conversation to collaborators May 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Extensions-Caching feature-request needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration
Projects
None yet
Development

No branches or pull requests

8 participants