Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup HybridCache public API #55732

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/Caching/Hybrid/src/HybridCacheBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Configuration extension methods for <see cref="IHybridCacheBuilder"/> / <see cref="HybridCache"/>.
Expand All @@ -14,7 +14,7 @@ public static class HybridCacheBuilderExtensions
/// <summary>
/// Serialize values of type <typeparamref name="T"/> with the specified serializer from <paramref name="serializer"/>.
/// </summary>
public static IHybridCacheBuilder WithSerializer<T>(this IHybridCacheBuilder builder, IHybridCacheSerializer<T> serializer)
public static IHybridCacheBuilder AddSerializer<T>(this IHybridCacheBuilder builder, IHybridCacheSerializer<T> serializer)
{
builder.Services.AddSingleton<IHybridCacheSerializer<T>>(serializer);
return builder;
Expand All @@ -23,7 +23,7 @@ public static IHybridCacheBuilder WithSerializer<T>(this IHybridCacheBuilder bui
/// <summary>
/// Serialize values of type <typeparamref name="T"/> with the serializer of type <typeparamref name="TImplementation"/>.
/// </summary>
public static IHybridCacheBuilder WithSerializer<T,
public static IHybridCacheBuilder AddSerializer<T,
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
Expand All @@ -37,7 +37,7 @@ public static IHybridCacheBuilder WithSerializer<T>(this IHybridCacheBuilder bui
/// <summary>
/// Add <paramref name="factory"/> as an additional serializer factory, which can provide serializers for multiple types.
/// </summary>
public static IHybridCacheBuilder WithSerializerFactory(this IHybridCacheBuilder builder, IHybridCacheSerializerFactory factory)
public static IHybridCacheBuilder AddSerializerFactory(this IHybridCacheBuilder builder, IHybridCacheSerializerFactory factory)
{
builder.Services.AddSingleton<IHybridCacheSerializerFactory>(factory);
return builder;
Expand All @@ -46,7 +46,7 @@ public static IHybridCacheBuilder WithSerializerFactory(this IHybridCacheBuilder
/// <summary>
/// Add a factory of type <typeparamref name="TImplementation"/> as an additional serializer factory, which can provide serializers for multiple types.
/// </summary>
public static IHybridCacheBuilder WithSerializerFactory<
public static IHybridCacheBuilder AddSerializerFactory<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/Caching/Hybrid/src/HybridCacheServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Configuration extension methods for <see cref="HybridCache"/>.
Expand Down
4 changes: 2 additions & 2 deletions src/Caching/Hybrid/src/Internal/DefaultHybridCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ public override ValueTask<T> GetOrCreateAsync<TState, T>(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<T>(string key, T value, HybridCacheEntryOptions? options = null, IReadOnlyCollection<string>? tags = null, CancellationToken token = default)
Expand Down
24 changes: 12 additions & 12 deletions src/Caching/Hybrid/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#nullable enable
abstract Microsoft.Extensions.Caching.Hybrid.HybridCache.GetOrCreateAsync<TState, T>(string! key, TState state, System.Func<TState, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask<T>>! factory, Microsoft.Extensions.Caching.Hybrid.HybridCacheEntryOptions? options = null, System.Collections.Generic.IReadOnlyCollection<string!>? tags = null, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask<T>
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<T>(string! key, T value, Microsoft.Extensions.Caching.Hybrid.HybridCacheEntryOptions? options = null, System.Collections.Generic.IReadOnlyCollection<string!>? 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<byte> value, Microsoft.Extensions.Caching.Distributed.DistributedCacheEntryOptions! options) -> void
Expand All @@ -11,7 +11,6 @@ Microsoft.Extensions.Caching.Distributed.IBufferDistributedCache.TryGetAsync(str
Microsoft.Extensions.Caching.Hybrid.HybridCache
Microsoft.Extensions.Caching.Hybrid.HybridCache.GetOrCreateAsync<T>(string! key, System.Func<System.Threading.CancellationToken, System.Threading.Tasks.ValueTask<T>>! factory, Microsoft.Extensions.Caching.Hybrid.HybridCacheEntryOptions? options = null, System.Collections.Generic.IReadOnlyCollection<string!>? tags = null, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask<T>
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
Expand Down Expand Up @@ -42,19 +41,20 @@ 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<T>
Microsoft.Extensions.Caching.Hybrid.IHybridCacheSerializer<T>.Deserialize(System.Buffers.ReadOnlySequence<byte> source) -> T
Microsoft.Extensions.Caching.Hybrid.IHybridCacheSerializer<T>.Serialize(T value, System.Buffers.IBufferWriter<byte>! target) -> void
Microsoft.Extensions.Caching.Hybrid.IHybridCacheSerializerFactory
Microsoft.Extensions.Caching.Hybrid.IHybridCacheSerializerFactory.TryCreateSerializer<T>(out Microsoft.Extensions.Caching.Hybrid.IHybridCacheSerializer<T>? serializer) -> bool
static Microsoft.Extensions.Caching.Hybrid.HybridCacheBuilderExtensions.WithSerializer<T, TImplementation>(this Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! builder) -> Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder!
static Microsoft.Extensions.Caching.Hybrid.HybridCacheBuilderExtensions.WithSerializer<T>(this Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! builder, Microsoft.Extensions.Caching.Hybrid.IHybridCacheSerializer<T>! 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<TImplementation>(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<Microsoft.Extensions.Caching.Hybrid.HybridCacheOptions!>! setupAction) -> Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder!
virtual Microsoft.Extensions.Caching.Hybrid.HybridCache.RemoveKeysAsync(System.Collections.Generic.IEnumerable<string!>! keys, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask
virtual Microsoft.Extensions.Caching.Hybrid.HybridCache.RemoveTagsAsync(System.Collections.Generic.IEnumerable<string!>! 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<T, TImplementation>(this Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! builder) -> Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder!
static Microsoft.Extensions.DependencyInjection.HybridCacheBuilderExtensions.AddSerializer<T>(this Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder! builder, Microsoft.Extensions.Caching.Hybrid.IHybridCacheSerializer<T>! 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<TImplementation>(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<Microsoft.Extensions.Caching.Hybrid.HybridCacheOptions!>! setupAction) -> Microsoft.Extensions.Caching.Hybrid.IHybridCacheBuilder!
virtual Microsoft.Extensions.Caching.Hybrid.HybridCache.RemoveAsync(System.Collections.Generic.IEnumerable<string!>! keys, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask
virtual Microsoft.Extensions.Caching.Hybrid.HybridCache.RemoveByTagAsync(System.Collections.Generic.IEnumerable<string!>! tags, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask
20 changes: 12 additions & 8 deletions src/Caching/Hybrid/src/Runtime/HybridCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,21 @@ private static class WrappedCallbackCache<T> // per-T memoized helper that allow
/// <summary>
/// Asynchronously removes the value associated with the key if it exists.
/// </summary>
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);

/// <summary>
/// Asynchronously removes the value associated with the key if it exists.
/// </summary>
/// <remarks>Implementors should treat <c>null</c> as empty</remarks>
public virtual ValueTask RemoveKeysAsync(IEnumerable<string> 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<string> keys, CancellationToken token = default)
{
return keys switch
{
// for consistency with GetOrCreate/Set: interpret null as "none"
null or ICollection<string> { Count: 0 } => default,
ICollection<string> { Count: 1 } => RemoveTagAsync(keys.Single(), token),
ICollection<string> { Count: 1 } => RemoveByTagAsync(keys.Single(), token),
_ => ForEachAsync(this, keys, token),
};

Expand All @@ -88,7 +90,7 @@ static async ValueTask ForEachAsync(HybridCache @this, IEnumerable<string> keys,
{
foreach (var key in keys)
{
await @this.RemoveKeyAsync(key, token).ConfigureAwait(false);
await @this.RemoveAsync(key, token).ConfigureAwait(false);
}
}
}
Expand All @@ -97,13 +99,14 @@ static async ValueTask ForEachAsync(HybridCache @this, IEnumerable<string> keys,
/// Asynchronously removes all values associated with the specified tags.
/// </summary>
/// <remarks>Implementors should treat <c>null</c> as empty</remarks>
public virtual ValueTask RemoveTagsAsync(IEnumerable<string> 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<string> tags, CancellationToken token = default)
{
return tags switch
{
// for consistency with GetOrCreate/Set: interpret null as "none"
null or ICollection<string> { Count: 0 } => default,
ICollection<string> { Count: 1 } => RemoveTagAsync(tags.Single(), token),
ICollection<string> { Count: 1 } => RemoveByTagAsync(tags.Single(), token),
_ => ForEachAsync(this, tags, token),
};

Expand All @@ -112,13 +115,14 @@ static async ValueTask ForEachAsync(HybridCache @this, IEnumerable<string> keys,
{
foreach (var key in keys)
{
await @this.RemoveTagAsync(key, token).ConfigureAwait(false);
await @this.RemoveByTagAsync(key, token).ConfigureAwait(false);
}
}
}

/// <summary>
/// Asynchronously removes all values associated with the specified tag.
/// </summary>
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);
}
6 changes: 3 additions & 3 deletions src/Caching/Hybrid/test/BufferReleaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -138,7 +138,7 @@ static bool Write(IBufferWriter<byte> 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
Expand Down Expand Up @@ -196,7 +196,7 @@ static bool Write(IBufferWriter<byte> 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
Expand Down
4 changes: 2 additions & 2 deletions src/Caching/Hybrid/test/L2Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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...");
Expand Down Expand Up @@ -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...");
Expand Down
4 changes: 2 additions & 2 deletions src/Caching/Hybrid/test/ServiceConstructionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void DefaultSerializerConfiguration()
public void CustomSerializerConfiguration()
{
var services = new ServiceCollection();
services.AddHybridCache().WithSerializer<Customer, CustomerSerializer>();
services.AddHybridCache().AddSerializer<Customer, CustomerSerializer>();
using var provider = services.BuildServiceProvider();
var cache = Assert.IsType<DefaultHybridCache>(provider.GetRequiredService<HybridCache>());

Expand All @@ -132,7 +132,7 @@ public void CustomSerializerConfiguration()
public void CustomSerializerFactoryConfiguration()
{
var services = new ServiceCollection();
services.AddHybridCache().WithSerializerFactory<CustomFactory>();
services.AddHybridCache().AddSerializerFactory<CustomFactory>();
using var provider = services.BuildServiceProvider();
var cache = Assert.IsType<DefaultHybridCache>(provider.GetRequiredService<HybridCache>());

Expand Down
Loading