Skip to content

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

Closed

Description

@KamranShahid

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

Activity

  1. KamranShahid commented on May 6, 2020

    @KamranShahid
    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;
            }
    
  2. jodydonetti commented on May 6, 2020

    @jodydonetti
    Contributor

    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.

  3. KamranShahid commented on May 6, 2020

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

  4. analogrelay commented on May 6, 2020

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

  5. KamranShahid commented on May 7, 2020

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

  6. Dotnet-GitSync-Bot commented on May 7, 2020

    @Dotnet-GitSync-Bot
    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.

  7. added this to the Future milestone on May 7, 2020
  8. removed
    untriagedNew issue has not been triaged by the area owner
    on Jul 1, 2020
  9. specialforest commented on Aug 4, 2020

    @specialforest

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

  10. maryamariyan commented on Oct 7, 2020

    @maryamariyan
    Contributor

    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.

  11. KamranShahid commented on Oct 14, 2020

    @KamranShahid
    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?

  12. sigmarsson commented on Mar 25, 2021

    @sigmarsson

    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 ?

  13. adamsitnik commented on Mar 26, 2021

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

  14. KamranShahid commented on Apr 14, 2021

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

  15. ghost added
    needs-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration
    on Apr 14, 2021
  16. maryamariyan commented on Apr 23, 2021

    @maryamariyan
    Contributor

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

  17. ghost locked as resolved and limited conversation to collaborators on May 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions