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
How to get the list of keys using IDistributedCache extension. #36402
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.
Reacted by Stefan Kip, john, Vladimir Panchenko, Raúl, Federico Javier Lopez Bertoni, Brandon J. Boone, Marc Asmar, Guilherme Britz Videira, Henrique Diniz, Anton Ganhammar and 29 more
+1 for this request. Any plan to add this support in the near future?
Reacted by Hamza Faran, Meysam Navaei, Cihan Güllü, Confusingboat, ralftar, one-matrix, Viktor Blöndal, TheTedster, Vladimir Panchenko, Raúl and 24 more
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.
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.
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.
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.
Reacted by Maryam Ariyan, David Fowler, David Wilson, Alan Willis, Marek Ištvánek, Dave Black, Vadim Ovchinnikov, Sanjay D, Pradeep Gururani, D. Turco and 3 more
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?
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).
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.
Activity
chuanboz commentedon Apr 24, 2019
+1 for this request. Any plan to add this support in the near future?
stefankip commentedon Aug 9, 2019
Too bad nobody cares to help is with this 👎
skironDotNet commentedon Jan 8, 2020
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.
dropsonic commentedon Mar 24, 2020
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
IDistributedCachehides 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.maryamariyan commentedon Oct 7, 2020
I believe this would be a new feature request.
cc: @Tratcher @eerhardt
Tratcher commentedon Oct 7, 2020
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.
eerhardt commentedon Oct 7, 2020
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.
rickdgray commentedon May 23, 2022
How is this still not implemented? Disappointed.
6 remaining items
wilkovanderveen commentedon Feb 8, 2023
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?
davidhenley commentedon Mar 6, 2023
Is this still not done after 5 years? I guess back to using the IConnectionMultiplexer itself
acandaldev commentedon May 10, 2023
With a concurrentDictionary and adding deleting in Get and Remove can be done?
I did my personal implementation of this
mgravell commentedon Feb 29, 2024
net9-timescale update; we're discussing a range of
IDistributedCachechanges 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 thatIDistributedCacheis not a particularly useful API, and the plan is to add a higher-levelHybridCachewhich usesIDistributedCacheas a backend. With that perspective, it means that we could also add some optionalISomeNewKeysWhateverAPI that the backend can implement if appropriate, and a suitable "get keys if you can" method onHybridCache. 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
IConnectionMultiplexerin 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 someISomeNewKeysWhateverthatHybridCacheknows about" would generalize, but would still be dependent on individual backends adding support (we would help with that for some backends).rosebyte commentedon Mar 11, 2026
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.
rickdgray-dc commentedon Mar 11, 2026
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.