diff --git a/src/Caching/Hybrid/src/HybridCacheBuilderExtensions.cs b/src/Caching/Hybrid/src/HybridCacheBuilderExtensions.cs index 4c7dc27c72e2..5ab0768496ef 100644 --- a/src/Caching/Hybrid/src/HybridCacheBuilderExtensions.cs +++ b/src/Caching/Hybrid/src/HybridCacheBuilderExtensions.cs @@ -2,9 +2,9 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics.CodeAnalysis; -using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Caching.Hybrid; -namespace Microsoft.Extensions.Caching.Hybrid; +namespace Microsoft.Extensions.DependencyInjection; /// /// Configuration extension methods for / . @@ -14,7 +14,7 @@ public static class HybridCacheBuilderExtensions /// /// Serialize values of type with the specified serializer from . /// - public static IHybridCacheBuilder WithSerializer(this IHybridCacheBuilder builder, IHybridCacheSerializer serializer) + public static IHybridCacheBuilder AddSerializer(this IHybridCacheBuilder builder, IHybridCacheSerializer serializer) { builder.Services.AddSingleton>(serializer); return builder; @@ -23,7 +23,7 @@ public static IHybridCacheBuilder WithSerializer(this IHybridCacheBuilder bui /// /// Serialize values of type with the serializer of type . /// - public static IHybridCacheBuilder WithSerializer(this IHybridCacheBuilder bui /// /// Add as an additional serializer factory, which can provide serializers for multiple types. /// - public static IHybridCacheBuilder WithSerializerFactory(this IHybridCacheBuilder builder, IHybridCacheSerializerFactory factory) + public static IHybridCacheBuilder AddSerializerFactory(this IHybridCacheBuilder builder, IHybridCacheSerializerFactory factory) { builder.Services.AddSingleton(factory); return builder; @@ -46,7 +46,7 @@ public static IHybridCacheBuilder WithSerializerFactory(this IHybridCacheBuilder /// /// Add a factory of type as an additional serializer factory, which can provide serializers for multiple types. /// - public static IHybridCacheBuilder WithSerializerFactory< + public static IHybridCacheBuilder AddSerializerFactory< #if NET5_0_OR_GREATER [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] #endif diff --git a/src/Caching/Hybrid/src/HybridCacheServiceExtensions.cs b/src/Caching/Hybrid/src/HybridCacheServiceExtensions.cs index 87bdfd1cda1a..f8aedbfe95ee 100644 --- a/src/Caching/Hybrid/src/HybridCacheServiceExtensions.cs +++ b/src/Caching/Hybrid/src/HybridCacheServiceExtensions.cs @@ -2,11 +2,11 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using Microsoft.Extensions.Caching.Hybrid; using Microsoft.Extensions.Caching.Hybrid.Internal; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; -namespace Microsoft.Extensions.Caching.Hybrid; +namespace Microsoft.Extensions.DependencyInjection; /// /// Configuration extension methods for . diff --git a/src/Caching/Hybrid/src/Internal/DefaultHybridCache.cs b/src/Caching/Hybrid/src/Internal/DefaultHybridCache.cs index 171140c4b722..a41c42ba635b 100644 --- a/src/Caching/Hybrid/src/Internal/DefaultHybridCache.cs +++ b/src/Caching/Hybrid/src/Internal/DefaultHybridCache.cs @@ -144,13 +144,13 @@ public override ValueTask GetOrCreateAsync(string key, TState stat return stampede.JoinAsync(token); } - public override ValueTask RemoveKeyAsync(string key, CancellationToken token = default) + public override ValueTask RemoveAsync(string key, CancellationToken token = default) { _localCache.Remove(key); return _backendCache is null ? default : new(_backendCache.RemoveAsync(key, token)); } - public override ValueTask RemoveTagAsync(string tag, CancellationToken token = default) + public override ValueTask RemoveByTagAsync(string tag, CancellationToken token = default) => default; // tags not yet implemented public override ValueTask SetAsync(string key, T value, HybridCacheEntryOptions? options = null, IReadOnlyCollection? tags = null, CancellationToken token = default) diff --git a/src/Caching/Hybrid/src/PublicAPI.Unshipped.txt b/src/Caching/Hybrid/src/PublicAPI.Unshipped.txt index 47e46d6d30ce..19682fb4a0f2 100644 --- a/src/Caching/Hybrid/src/PublicAPI.Unshipped.txt +++ b/src/Caching/Hybrid/src/PublicAPI.Unshipped.txt @@ -1,7 +1,7 @@ #nullable enable abstract Microsoft.Extensions.Caching.Hybrid.HybridCache.GetOrCreateAsync(string! key, TState state, System.Func>! factory, Microsoft.Extensions.Caching.Hybrid.HybridCacheEntryOptions? options = null, System.Collections.Generic.IReadOnlyCollection? tags = null, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask -abstract Microsoft.Extensions.Caching.Hybrid.HybridCache.RemoveKeyAsync(string! key, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask -abstract Microsoft.Extensions.Caching.Hybrid.HybridCache.RemoveTagAsync(string! tag, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask +abstract Microsoft.Extensions.Caching.Hybrid.HybridCache.RemoveAsync(string! key, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask +abstract Microsoft.Extensions.Caching.Hybrid.HybridCache.RemoveByTagAsync(string! tag, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask abstract Microsoft.Extensions.Caching.Hybrid.HybridCache.SetAsync(string! key, T value, Microsoft.Extensions.Caching.Hybrid.HybridCacheEntryOptions? options = null, System.Collections.Generic.IReadOnlyCollection? tags = null, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask Microsoft.Extensions.Caching.Distributed.IBufferDistributedCache Microsoft.Extensions.Caching.Distributed.IBufferDistributedCache.Set(string! key, System.Buffers.ReadOnlySequence value, Microsoft.Extensions.Caching.Distributed.DistributedCacheEntryOptions! options) -> void @@ -11,7 +11,6 @@ Microsoft.Extensions.Caching.Distributed.IBufferDistributedCache.TryGetAsync(str Microsoft.Extensions.Caching.Hybrid.HybridCache Microsoft.Extensions.Caching.Hybrid.HybridCache.GetOrCreateAsync(string! key, System.Func>! factory, Microsoft.Extensions.Caching.Hybrid.HybridCacheEntryOptions? options = null, System.Collections.Generic.IReadOnlyCollection? tags = null, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask Microsoft.Extensions.Caching.Hybrid.HybridCache.HybridCache() -> void -Microsoft.Extensions.Caching.Hybrid.HybridCacheBuilderExtensions Microsoft.Extensions.Caching.Hybrid.HybridCacheEntryFlags Microsoft.Extensions.Caching.Hybrid.HybridCacheEntryFlags.DisableCompression = 32 -> Microsoft.Extensions.Caching.Hybrid.HybridCacheEntryFlags Microsoft.Extensions.Caching.Hybrid.HybridCacheEntryFlags.DisableDistributedCache = Microsoft.Extensions.Caching.Hybrid.HybridCacheEntryFlags.DisableDistributedCacheRead | Microsoft.Extensions.Caching.Hybrid.HybridCacheEntryFlags.DisableDistributedCacheWrite -> Microsoft.Extensions.Caching.Hybrid.HybridCacheEntryFlags @@ -42,7 +41,6 @@ Microsoft.Extensions.Caching.Hybrid.HybridCacheOptions.MaximumPayloadBytes.get - Microsoft.Extensions.Caching.Hybrid.HybridCacheOptions.MaximumPayloadBytes.set -> void Microsoft.Extensions.Caching.Hybrid.HybridCacheOptions.ReportTagMetrics.get -> bool Microsoft.Extensions.Caching.Hybrid.HybridCacheOptions.ReportTagMetrics.set -> void -Microsoft.Extensions.Caching.Hybrid.HybridCacheServiceExtensions Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder.Services.get -> Microsoft.Extensions.DependencyInjection.IServiceCollection! Microsoft.Extensions.Caching.Hybrid.IHybridCacheSerializer @@ -50,11 +48,13 @@ Microsoft.Extensions.Caching.Hybrid.IHybridCacheSerializer.Deserialize(System Microsoft.Extensions.Caching.Hybrid.IHybridCacheSerializer.Serialize(T value, System.Buffers.IBufferWriter! target) -> void Microsoft.Extensions.Caching.Hybrid.IHybridCacheSerializerFactory Microsoft.Extensions.Caching.Hybrid.IHybridCacheSerializerFactory.TryCreateSerializer(out Microsoft.Extensions.Caching.Hybrid.IHybridCacheSerializer? serializer) -> bool -static Microsoft.Extensions.Caching.Hybrid.HybridCacheBuilderExtensions.WithSerializer(this Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! builder) -> Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! -static Microsoft.Extensions.Caching.Hybrid.HybridCacheBuilderExtensions.WithSerializer(this Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! builder, Microsoft.Extensions.Caching.Hybrid.IHybridCacheSerializer! serializer) -> Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! -static Microsoft.Extensions.Caching.Hybrid.HybridCacheBuilderExtensions.WithSerializerFactory(this Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! builder, Microsoft.Extensions.Caching.Hybrid.IHybridCacheSerializerFactory! factory) -> Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! -static Microsoft.Extensions.Caching.Hybrid.HybridCacheBuilderExtensions.WithSerializerFactory(this Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! builder) -> Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! -static Microsoft.Extensions.Caching.Hybrid.HybridCacheServiceExtensions.AddHybridCache(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! -static Microsoft.Extensions.Caching.Hybrid.HybridCacheServiceExtensions.AddHybridCache(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services, System.Action! setupAction) -> Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! -virtual Microsoft.Extensions.Caching.Hybrid.HybridCache.RemoveKeysAsync(System.Collections.Generic.IEnumerable! keys, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask -virtual Microsoft.Extensions.Caching.Hybrid.HybridCache.RemoveTagsAsync(System.Collections.Generic.IEnumerable! tags, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask +Microsoft.Extensions.DependencyInjection.HybridCacheBuilderExtensions +Microsoft.Extensions.DependencyInjection.HybridCacheServiceExtensions +static Microsoft.Extensions.DependencyInjection.HybridCacheBuilderExtensions.AddSerializer(this Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! builder) -> Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! +static Microsoft.Extensions.DependencyInjection.HybridCacheBuilderExtensions.AddSerializer(this Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! builder, Microsoft.Extensions.Caching.Hybrid.IHybridCacheSerializer! serializer) -> Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! +static Microsoft.Extensions.DependencyInjection.HybridCacheBuilderExtensions.AddSerializerFactory(this Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! builder, Microsoft.Extensions.Caching.Hybrid.IHybridCacheSerializerFactory! factory) -> Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! +static Microsoft.Extensions.DependencyInjection.HybridCacheBuilderExtensions.AddSerializerFactory(this Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! builder) -> Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! +static Microsoft.Extensions.DependencyInjection.HybridCacheServiceExtensions.AddHybridCache(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! +static Microsoft.Extensions.DependencyInjection.HybridCacheServiceExtensions.AddHybridCache(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services, System.Action! setupAction) -> Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! +virtual Microsoft.Extensions.Caching.Hybrid.HybridCache.RemoveAsync(System.Collections.Generic.IEnumerable! keys, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask +virtual Microsoft.Extensions.Caching.Hybrid.HybridCache.RemoveByTagAsync(System.Collections.Generic.IEnumerable! tags, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask diff --git a/src/Caching/Hybrid/src/Runtime/HybridCache.cs b/src/Caching/Hybrid/src/Runtime/HybridCache.cs index d2ba7f809cd4..090da99bf867 100644 --- a/src/Caching/Hybrid/src/Runtime/HybridCache.cs +++ b/src/Caching/Hybrid/src/Runtime/HybridCache.cs @@ -67,19 +67,21 @@ private static class WrappedCallbackCache // per-T memoized helper that allow /// /// Asynchronously removes the value associated with the key if it exists. /// - public abstract ValueTask RemoveKeyAsync(string key, CancellationToken token = default); + [System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "Not ambiguous in context")] + public abstract ValueTask RemoveAsync(string key, CancellationToken token = default); /// /// Asynchronously removes the value associated with the key if it exists. /// /// Implementors should treat null as empty - public virtual ValueTask RemoveKeysAsync(IEnumerable keys, CancellationToken token = default) + [System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "Not ambiguous in context")] + public virtual ValueTask RemoveAsync(IEnumerable keys, CancellationToken token = default) { return keys switch { // for consistency with GetOrCreate/Set: interpret null as "none" null or ICollection { Count: 0 } => default, - ICollection { Count: 1 } => RemoveTagAsync(keys.Single(), token), + ICollection { Count: 1 } => RemoveByTagAsync(keys.Single(), token), _ => ForEachAsync(this, keys, token), }; @@ -88,7 +90,7 @@ static async ValueTask ForEachAsync(HybridCache @this, IEnumerable keys, { foreach (var key in keys) { - await @this.RemoveKeyAsync(key, token).ConfigureAwait(false); + await @this.RemoveAsync(key, token).ConfigureAwait(false); } } } @@ -97,13 +99,14 @@ static async ValueTask ForEachAsync(HybridCache @this, IEnumerable keys, /// Asynchronously removes all values associated with the specified tags. /// /// Implementors should treat null as empty - public virtual ValueTask RemoveTagsAsync(IEnumerable tags, CancellationToken token = default) + [System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "Not ambiguous in context")] + public virtual ValueTask RemoveByTagAsync(IEnumerable tags, CancellationToken token = default) { return tags switch { // for consistency with GetOrCreate/Set: interpret null as "none" null or ICollection { Count: 0 } => default, - ICollection { Count: 1 } => RemoveTagAsync(tags.Single(), token), + ICollection { Count: 1 } => RemoveByTagAsync(tags.Single(), token), _ => ForEachAsync(this, tags, token), }; @@ -112,7 +115,7 @@ static async ValueTask ForEachAsync(HybridCache @this, IEnumerable keys, { foreach (var key in keys) { - await @this.RemoveTagAsync(key, token).ConfigureAwait(false); + await @this.RemoveByTagAsync(key, token).ConfigureAwait(false); } } } @@ -120,5 +123,6 @@ static async ValueTask ForEachAsync(HybridCache @this, IEnumerable keys, /// /// Asynchronously removes all values associated with the specified tag. /// - public abstract ValueTask RemoveTagAsync(string tag, CancellationToken token = default); + [System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "Not ambiguous in context")] + public abstract ValueTask RemoveByTagAsync(string tag, CancellationToken token = default); } diff --git a/src/Caching/Hybrid/test/BufferReleaseTests.cs b/src/Caching/Hybrid/test/BufferReleaseTests.cs index 5a8c0a8029c7..046cb23d0762 100644 --- a/src/Caching/Hybrid/test/BufferReleaseTests.cs +++ b/src/Caching/Hybrid/test/BufferReleaseTests.cs @@ -53,7 +53,7 @@ public async Task BufferGetsReleased_NoL2() Assert.NotSame(first, second); Assert.Equal(1, cacheItem.RefCount); - await cache.RemoveKeyAsync(key); + await cache.RemoveAsync(key); var third = await cache.GetOrCreateAsync(key, _ => GetAsync(), _noUnderlying); Assert.Null(third); @@ -138,7 +138,7 @@ static bool Write(IBufferWriter destination, byte[]? buffer) Assert.NotSame(first, second); Assert.Equal(1, cacheItem.RefCount); - await cache.RemoveKeyAsync(key); + await cache.RemoveAsync(key); var third = await cache.GetOrCreateAsync(key, _ => GetAsync(), _noUnderlying); Assert.Null(third); Assert.Null(await cache.BackendCache.GetAsync(key)); // should be gone from L2 too @@ -196,7 +196,7 @@ static bool Write(IBufferWriter destination, byte[]? buffer) Assert.NotSame(first, second); Assert.Equal(1, cacheItem.RefCount); - await cache.RemoveKeyAsync(key); + await cache.RemoveAsync(key); var third = await cache.GetOrCreateAsync(key, _ => GetAsync(), _noUnderlying); Assert.Null(third); Assert.Null(await cache.BackendCache.GetAsync(key)); // should be gone from L2 too diff --git a/src/Caching/Hybrid/test/L2Tests.cs b/src/Caching/Hybrid/test/L2Tests.cs index 7d8389f03b7f..6fd77c0707a6 100644 --- a/src/Caching/Hybrid/test/L2Tests.cs +++ b/src/Caching/Hybrid/test/L2Tests.cs @@ -81,7 +81,7 @@ public async Task AssertL2Operations_Immutable(bool buffers) Assert.Equal(8, backend.OpCount); // SET Log.WriteLine("Removing key..."); - await cache.RemoveKeyAsync(Me()); + await cache.RemoveAsync(Me()); Assert.Equal(9, backend.OpCount); // DEL Log.WriteLine("Fetching new..."); @@ -136,7 +136,7 @@ public async Task AssertL2Operations_Mutable(bool buffers) Assert.Equal(8, backend.OpCount); // SET Log.WriteLine("Removing key..."); - await cache.RemoveKeyAsync(Me()); + await cache.RemoveAsync(Me()); Assert.Equal(9, backend.OpCount); // DEL Log.WriteLine("Fetching new..."); diff --git a/src/Caching/Hybrid/test/ServiceConstructionTests.cs b/src/Caching/Hybrid/test/ServiceConstructionTests.cs index 50887d990aa0..103ac87ed890 100644 --- a/src/Caching/Hybrid/test/ServiceConstructionTests.cs +++ b/src/Caching/Hybrid/test/ServiceConstructionTests.cs @@ -120,7 +120,7 @@ public void DefaultSerializerConfiguration() public void CustomSerializerConfiguration() { var services = new ServiceCollection(); - services.AddHybridCache().WithSerializer(); + services.AddHybridCache().AddSerializer(); using var provider = services.BuildServiceProvider(); var cache = Assert.IsType(provider.GetRequiredService()); @@ -132,7 +132,7 @@ public void CustomSerializerConfiguration() public void CustomSerializerFactoryConfiguration() { var services = new ServiceCollection(); - services.AddHybridCache().WithSerializerFactory(); + services.AddHybridCache().AddSerializerFactory(); using var provider = services.BuildServiceProvider(); var cache = Assert.IsType(provider.GetRequiredService());