Skip to content

Bulk remove on IDistributedCache #36568

Open

Description

@ehabelgindy

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.

Activity

  1. RehanSaeed commented on Apr 12, 2016

    @RehanSaeed

    I posted an IDistributedCache helper which clears the cache and also bulk removes keys here, just in case someone finds it useful.

  2. SimonOrdo commented on Aug 26, 2018

    @SimonOrdo

    @RehanSaeed It looks like you're missing IDistributedCacheExtended definition in your post.

  3. added
    untriaged
    on May 15, 2020
  4. added this to the Future milestone on May 15, 2020
  5. removed
    untriaged
    on Jul 1, 2020
  6. modified the milestones: Future, 6.0.0 on Oct 7, 2020
  7. eerhardt commented on Jul 19, 2021

    @eerhardt
    Member

    We've reached feature complete for .NET 6. Moving to 7.

  8. modified the milestones: 6.0.0, 7.0.0 on Jul 19, 2021
  9. deeprobin commented on Jun 12, 2022

    @deeprobin
    Contributor

    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:

    namespace Microsoft.Extensions.Caching.Distributed
    {
        public interface IDistributedCache
        {
            void Clear();
            // Maybe, a ValueTask would not be a bad idea, but other Async APIs use a normal Task in this class.
            Task ClearAsync(CancellationToken cancellationToken = default);
        }
    
        public partial class MemoryDistributedCache : IDistributedCache {
            public void Clear();
            public Task ClearAsync(CancellationToken cancellationToken = default);
        }
    }
    
    namespace Microsoft.Extensions.Caching.Redis
    {
        public partial class RedisCache : IDisposable, IDistributedCache {
            public void Clear();
            public Task ClearAsync(CancellationToken cancellationToken = default);
        }
    }
    
    namespace Microsoft.Extensions.Caching.StackExchangeRedis
    {
        public partial class RedisCache : IDisposable, IDistributedCache {
            public void Clear();
            public Task ClearAsync(CancellationToken cancellationToken = default);
        }
    }
    
    namespace Microsoft.Extensions.Caching.SqlServer
    {
        public partial class SqlServerCache : IDistributedCache {
            public void Clear();
            public Task ClearAsync(CancellationToken cancellationToken = default);
        }
    }
  10. deeprobin commented on Jun 12, 2022

    @deeprobin
    Contributor
    • The interface can't be changed, per our breaking change rules.

    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.

  11. eerhardt commented on Jun 13, 2022

    @eerhardt
    Member

    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.

  12. modified the milestones: 7.0.0, 8.0.0 on Jun 14, 2022
  13. modified the milestones: 8.0.0, 9.0.0 on Jul 28, 2023
  14. modified the milestones: 9.0.0, Future on Jul 17, 2024
  15. rosebyte commented on Mar 11, 2026

    @rosebyte
    Member

    Triage: sounds reasonable.

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