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
Would it be useful if the IDistibutedCache interface can allow bulk remove?
Unlike the memory cache, removing multiple entries from distributed caches can be an expensive task. Providing an overload of the Remove function that accept an array of keys - e.g. Task Remove(string [] keys) - would allow more efficient implementations of distributed caches.
Reacted by RickBlouch, Ben Brandt, Viktor Svub, Glen, austinw-fineart and Leonardo Calzavara
MemoryCache already implements this (see #45593). I think we can tag this as api-suggestion. However, this API would also be a breaking-change, due to other non-BCL implementations of the interface.
Here my proposal:
namespaceMicrosoft.Extensions.Caching.Distributed{publicinterfaceIDistributedCache{voidClear();// Maybe, a ValueTask would not be a bad idea, but other Async APIs use a normal Task in this class.TaskClearAsync(CancellationTokencancellationToken=default);}publicpartialclassMemoryDistributedCache:IDistributedCache{publicvoidClear();publicTaskClearAsync(CancellationTokencancellationToken=default);}}namespaceMicrosoft.Extensions.Caching.Redis{publicpartialclassRedisCache:IDisposable,IDistributedCache{publicvoidClear();publicTaskClearAsync(CancellationTokencancellationToken=default);}}namespaceMicrosoft.Extensions.Caching.StackExchangeRedis{publicpartialclassRedisCache:IDisposable,IDistributedCache{publicvoidClear();publicTaskClearAsync(CancellationTokencancellationToken=default);}}namespaceMicrosoft.Extensions.Caching.SqlServer{publicpartialclassSqlServerCache:IDistributedCache{publicvoidClear();publicTaskClearAsync(CancellationTokencancellationToken=default);}}
We have started using Default Interface Members (DIMs) in Microsoft.Extensions. This may be a candidate to add in .NET 6.0+ APIs just like we did for #66479.
Here my proposal:
I don't believe the original intention of this issue is to remove all the entries from the cache, but remove a set of them specified by a collection of keys.
Activity
RehanSaeed commentedon Apr 12, 2016
I posted an
IDistributedCachehelper which clears the cache and also bulk removes keys here, just in case someone finds it useful.SimonOrdo commentedon Aug 26, 2018
@RehanSaeed It looks like you're missing IDistributedCacheExtended definition in your post.
eerhardt commentedon Jul 19, 2021
We've reached feature complete for .NET 6. Moving to 7.
deeprobin commentedon Jun 12, 2022
MemoryCachealready implements this (see #45593). I think we can tag this asapi-suggestion. However, this API would also be abreaking-change, due to other non-BCL implementations of the interface.Here my proposal:
deeprobin commentedon Jun 12, 2022
Originally posted by @bartonjs in #45593 (comment)
It would probably make sense to update only the implementations and not change the interface due to the breaking change.
eerhardt commentedon Jun 13, 2022
We have started using Default Interface Members (DIMs) in Microsoft.Extensions. This may be a candidate to add in .NET 6.0+ APIs just like we did for #66479.
I don't believe the original intention of this issue is to remove all the entries from the cache, but remove a set of them specified by a collection of keys.
rosebyte commentedon Mar 11, 2026
Triage: sounds reasonable.