Skip to content

HybridCache - rename RemoveKeyAsync and RemoveTagAsync to "By" #55332

Closed
@mgravell

Description

@mgravell

This is a .NET 9 feature that is in preview 4 but is not yet GA.

It was raised in post-merge feedback that the "remove" naming is a little unclear/ambiguous/inconsistent - in particular, for RemoveTagAsync, we're not "removing a tag", we're removing other things (cache entries) by their tag. Cross-reference: #53255

proposed:

  namespace Microsoft.Extensions.Caching.Hybrid;

  public abstract class HybridCache
  {
-     public abstract ValueTask RemoveKeyAsync(string key, CancellationToken token = default);
-     public virtual ValueTask RemoveKeysAsync(IEnumerable<string> keys, CancellationToken token = default)
-     public abstract ValueTask RemoveTagAsync(string tag, CancellationToken token = default);
-     public virtual ValueTask RemoveTagsAsync(IEnumerable<string> tags, CancellationToken token = default)

+     public abstract ValueTask RemoveByKeyAsync(string key, CancellationToken token = default);
+     public virtual ValueTask RemoveByKeyAsync(IEnumerable<string> keys, CancellationToken token = default)
+     public abstract ValueTask RemoveByTagAsync(string tag, CancellationToken token = default);
+     public virtual ValueTask RemoveByTagAsync(IEnumerable<string> tags, CancellationToken token = default)
  }

This adds the By nomenclature, and removes the plurality (RemoveByTag seems fine when taking multiple tags, and RemoveByTags prompts the question as to whether a cache-entry needs to have all the tags). The ambiguity of null (implicitly castable to string and IEnumerable<string>) is not important; this is invalid both in terms of the API (via NRT) and implementation (null is not really valid for either scanario; we have advised implementors to silently no-op null in the "tag" case, but we don't need to encourage it, and explicit null would already have been a compiler warning because of NRT).

Additionally, this makes the API more consistent with IOutputCacheStore, which uses the name EvictByTagAsync, i.e. uses the By naming. The Evict vs Remove is a separate discussion - happy to have that "now", but for comparison: IMemoryCache uses Remove; IDistributedCache uses Remove/RemoveAsync; our main objective here is to clarify the "what by?".

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedAPI was approved in API review, it can be implementedarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractionsfeature-cachingIncludes: StackExchangeRedis and SqlServer distributed caches

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions