Description
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?".