Skip to content

How to get the list of keys using IDistributedCache extension. #36402

Closed as not planned

Description

@RajivPandi

Is there an extension method available to get the list of keys from the cache.
I am using Redis Cache provider, i can able to get the list of keys matching the patter using Redis CLI. I am looking for similar feature using IDistributedCache.

Activity

  1. chuanboz commented on Apr 24, 2019

    @chuanboz

    +1 for this request. Any plan to add this support in the near future?

  2. stefankip commented on Aug 9, 2019

    @stefankip

    Too bad nobody cares to help is with this 👎

  3. skironDotNet commented on Jan 8, 2020

    @skironDotNet

    You could write your own. I know not a good answer but I'm looking for same thing, so I'm going to have my instance of IDistributedCache where I pass original IDistributedCache and low level Redis client, then will have a method to get keys, keeping ready features of IDistributedCache like sliding expiration management.

    Saying all that I'm supprised MS, didn't make this by deafult like KeyExists() or didn't expose Redis client as a property of IDistributedCache so it would be super easy to write extensions to use every feature of Redis.

  4. dropsonic commented on Mar 24, 2020

    @dropsonic

    It is a very important feature indeed.
    In my scenario, I have some features in every cache item (e.g., locale, username, etc.), and I want to invalidate the cache by a specific feature (e.g., invalidate all cache items where locale = "en-US").
    It is impossible to do with the current interface.

    @skironDotNet exposing Redis client is not a good idea because IDistributedCache hides implementation details from you. It might be Redis, but it also might be SQL or in-memory cache so you cannot use Redis-specific details.

  5. added
    untriaged
    on May 13, 2020
  6. added this to the Future milestone on May 13, 2020
  7. removed
    untriaged
    on Jul 1, 2020
  8. maryamariyan commented on Oct 7, 2020

    @maryamariyan
    Contributor

    I believe this would be a new feature request.

    cc: @Tratcher @eerhardt

  9. Tratcher commented on Oct 7, 2020

    @Tratcher
    Member

    Yes this would require new APIs. We've decided against this in the past for both IDistributedCache and IMemoryCache because of the ephemeral nature of cache entries. An entry name might be returned in the list, but then be removed and/or replaced before you could request it.

  10. eerhardt commented on Oct 7, 2020

    @eerhardt
    Member

    We've decided against this in the past for both IDistributedCache and IMemoryCache because of the ephemeral nature of cache entries.

    My thinking is that we could return a "snapshot" of the keys in the cache at the point in time you ask for them. There wouldn't be a guarantee that the key would still exist (or have the same value) after the call. The idea being you could use this new API in conjunction with #36568, to allow users to remove many entries in a single call. Given the snapshot, they can look through them (maybe apply Regex or StartsWith or similar), and build up a list of the entries they want to remove. This seems to be a pretty popular scenario - we saw a handful of issues discussing this same thing.

  11. rickdgray commented on May 23, 2022

    @rickdgray

    How is this still not implemented? Disappointed.

  12. removed this from the Future milestone on May 24, 2022
  13. 6 remaining items

  14. wilkovanderveen commented on Feb 8, 2023

    @wilkovanderveen

    So if I understand correctly, the only way to retrieve multiple cached items by an expression based on the key, is, for now, to know and use the concrete implementation behind the interface for allowing to get all keys which match a certain pattern?

  15. davidhenley commented on Mar 6, 2023

    @davidhenley

    Is this still not done after 5 years? I guess back to using the IConnectionMultiplexer itself

    var db = Redis.GetDatabase();
    var endpoint = Redis.GetEndPoints().First();
    var redisServer = Redis.GetServer(endpoint);
    await foreach (var key in redisServer.KeysAsync(pattern: $"{typeof(TEntity).Name}:list*"))
    {
        await db.KeyDeleteAsync(key);
    }
  16. acandaldev commented on May 10, 2023

    @acandaldev

    With a concurrentDictionary and adding deleting in Get and Remove can be done?
    I did my personal implementation of this

  17. modified the milestones: 8.0.0, 9.0.0 on Jul 28, 2023
  18. mgravell commented on Feb 29, 2024

    @mgravell
    Contributor

    net9-timescale update; we're discussing a range of IDistributedCache changes here; to be explicit, "list of keys" support is not currently on the "doing right now" list, but: the idea that we might want to support this is duly noted. The fundamental idea on that ticket is that IDistributedCache is not a particularly useful API, and the plan is to add a higher-level HybridCache which uses IDistributedCache as a backend. With that perspective, it means that we could also add some optional ISomeNewKeysWhatever API that the backend can implement if appropriate, and a suitable "get keys if you can" method on HybridCache. And since that is a base class rather than an interface, we can add methods without it breaking anything.

    However: I'll be honest - I'm not a huge fan of "list the keys" APIs; in reality, most times I've seen people use this kind of API: they've been using it inappropriately and in ways that are actively harmful to the system. Key-value stores usually aren't optimized for this scenario if it is even possible at all - they're optimized for "GET {key}" and "PUT {KEY} {value} {expiry}" (in the language of the backend). This usage might make sense for a "database admin / visualization" tool, but it isn't usually something you should use in routine/regular application code, IMO.

    I will note that Aspire makes it easier to also get to the "real" backend, such as IConnectionMultiplexer in the case of redis, and from there: there are ways of getting the keys - although it would be up to the consumer to enforce DB/prefix/etc complications, which are the things we would deal with on the caller's behalf if we did add direct support. Obviously this doesn't generalize - i.e. if you're using SQL or Cosmos or NCache or whatever: you'd need to start from scratch. The "add some ISomeNewKeysWhatever that HybridCache knows about" would generalize, but would still be dependent on individual backends adding support (we would help with that for some backends).

  19. modified the milestones: 9.0.0, Future on Jul 17, 2024
  20. modified the milestones: Future, 11.0.0 on Mar 11, 2026
  21. rosebyte commented on Mar 11, 2026

    @rosebyte
    Member

    Triage: we see the risks @mgravell mentions, when such a specific information is needed, you need to go to lower level API, moreover, given that not all distributed caches support getting all keys.

  22. rickdgray-dc commented on Mar 11, 2026

    @rickdgray-dc

    Over 7 years to be told it's not going to be implemented. Good to know my ex-coworkers from 2 jobs ago still will not be able to resolve that ticket.

  23. locked and limited conversation to collaborators on Apr 11, 2026
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

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions