Skip to content

Commit a4ca780

Browse files
committed
Rename MessagePack cache to Serialized cache
Renames `AddMessagePackDistributedCache` to `AddSerializedDistributedCache` method. Introduces an `Obsolete` attribute to the old method, guiding users toward the new, more generic name.
1 parent da44c5a commit a4ca780

3 files changed

Lines changed: 62 additions & 18 deletions

File tree

Neolution.Extensions.Caching.Distributed/ServiceCollectionExtensions.cs

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Microsoft.Extensions.DependencyInjection
33
{
44
using System;
5+
using System.Linq;
56
using Neolution.Extensions.Caching.Abstractions;
67
using Neolution.Extensions.Caching.Distributed;
78

@@ -11,39 +12,82 @@ namespace Microsoft.Extensions.DependencyInjection
1112
public static class ServiceCollectionExtensions
1213
{
1314
/// <summary>
14-
/// Adds the distributed caching implementation that uses MessagePack for serialization.
15+
/// Adds the serialized distributed caching implementation with MessagePack serialization.
1516
/// Requires an <see cref="Microsoft.Extensions.Caching.Distributed.IDistributedCache"/>
1617
/// provider to be registered (e.g., Redis, SQL Server, Memory).
1718
/// </summary>
1819
/// <param name="services">The service collection.</param>
1920
/// <returns>The service collection for fluent chaining.</returns>
20-
public static IServiceCollection AddMessagePackDistributedCache(this IServiceCollection services)
21+
/// <exception cref="InvalidOperationException">Thrown when no IDistributedCache provider is registered.</exception>
22+
public static IServiceCollection AddSerializedDistributedCache(this IServiceCollection services)
2123
{
22-
return services.AddMessagePackDistributedCache(_ => { });
24+
return services.AddSerializedDistributedCache(_ => { });
2325
}
2426

2527
/// <summary>
26-
/// Adds the distributed caching implementation that uses MessagePack for serialization,
27-
/// with custom configuration options.
28+
/// Adds the serialized distributed caching implementation with MessagePack serialization
29+
/// and custom configuration options.
2830
/// Requires an <see cref="Microsoft.Extensions.Caching.Distributed.IDistributedCache"/>
2931
/// provider to be registered (e.g., Redis, SQL Server, Memory).
3032
/// </summary>
3133
/// <param name="services">The service collection.</param>
3234
/// <param name="configureOptions">The action to configure cache options.</param>
3335
/// <returns>The service collection for fluent chaining.</returns>
3436
/// <exception cref="ArgumentNullException">Thrown when configureOptions is null.</exception>
35-
public static IServiceCollection AddMessagePackDistributedCache(this IServiceCollection services, Action<MessagePackDistributedCacheOptions> configureOptions)
37+
/// <exception cref="InvalidOperationException">Thrown when no IDistributedCache provider is registered.</exception>
38+
public static IServiceCollection AddSerializedDistributedCache(this IServiceCollection services, Action<MessagePackDistributedCacheOptions> configureOptions)
3639
{
3740
if (configureOptions == null)
3841
{
3942
throw new ArgumentNullException(nameof(configureOptions));
4043
}
4144

45+
// Validate that an IDistributedCache provider has been registered
46+
if (!services.Any(x => x.ServiceType == typeof(Microsoft.Extensions.Caching.Distributed.IDistributedCache)))
47+
{
48+
throw new InvalidOperationException(
49+
"""
50+
An IDistributedCache provider must be registered before calling AddSerializedDistributedCache().
51+
Register a provider such as Redis (AddStackExchangeRedisCache), SQL Server (AddDistributedSqlServerCache),
52+
or Memory (AddDistributedMemoryCache) first.
53+
"""
54+
);
55+
}
56+
4257
services.AddOptions();
4358
services.Configure(configureOptions);
4459
services.AddSingleton(typeof(IDistributedCache<>), typeof(MessagePackDistributedCache<>));
4560

4661
return services;
4762
}
63+
64+
/// <summary>
65+
/// Adds the distributed caching implementation that uses MessagePack for serialization.
66+
/// Requires an <see cref="Microsoft.Extensions.Caching.Distributed.IDistributedCache"/>
67+
/// provider to be registered (e.g., Redis, SQL Server, Memory).
68+
/// </summary>
69+
/// <param name="services">The service collection.</param>
70+
/// <returns>The service collection for fluent chaining.</returns>
71+
[Obsolete("Use AddSerializedDistributedCache() instead. This method will be removed in a future version.")]
72+
public static IServiceCollection AddMessagePackDistributedCache(this IServiceCollection services)
73+
{
74+
return services.AddSerializedDistributedCache();
75+
}
76+
77+
/// <summary>
78+
/// Adds the distributed caching implementation that uses MessagePack for serialization,
79+
/// with custom configuration options.
80+
/// Requires an <see cref="Microsoft.Extensions.Caching.Distributed.IDistributedCache"/>
81+
/// provider to be registered (e.g., Redis, SQL Server, Memory).
82+
/// </summary>
83+
/// <param name="services">The service collection.</param>
84+
/// <param name="configureOptions">The action to configure cache options.</param>
85+
/// <returns>The service collection for fluent chaining.</returns>
86+
/// <exception cref="ArgumentNullException">Thrown when configureOptions is null.</exception>
87+
[Obsolete("Use AddSerializedDistributedCache() instead. This method will be removed in a future version.")]
88+
public static IServiceCollection AddMessagePackDistributedCache(this IServiceCollection services, Action<MessagePackDistributedCacheOptions> configureOptions)
89+
{
90+
return services.AddSerializedDistributedCache(configureOptions);
91+
}
4892
}
4993
}

Neolution.Extensions.Caching.UnitTests/CacheKeyVersioningTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void MessagePackCacheKeyWithoutVersionByDefault()
2525
// Arrange
2626
var services = new ServiceCollection();
2727
services.AddDistributedMemoryCache();
28-
services.AddMessagePackDistributedCache(); // No options - should not include version
28+
services.AddSerializedDistributedCache(); // No options - should not include version
2929

3030
using var serviceProvider = services.BuildServiceProvider();
3131
var cache = serviceProvider.GetRequiredService<IDistributedCache<TestCacheId>>();
@@ -47,7 +47,7 @@ public void MessagePackCacheKeyIncludesDefaultVersion()
4747
// Arrange
4848
var services = new ServiceCollection();
4949
services.AddDistributedMemoryCache();
50-
services.AddMessagePackDistributedCache();
50+
services.AddSerializedDistributedCache();
5151

5252
using var serviceProvider = services.BuildServiceProvider();
5353
var cache = serviceProvider.GetRequiredService<IDistributedCache<TestCacheId>>();
@@ -69,7 +69,7 @@ public void MessagePackCacheKeyIncludesCustomVersion()
6969
// Arrange
7070
var services = new ServiceCollection();
7171
services.AddDistributedMemoryCache();
72-
services.AddMessagePackDistributedCache(options =>
72+
services.AddSerializedDistributedCache(options =>
7373
{
7474
options.Version = 2;
7575
});
@@ -94,11 +94,11 @@ public void DifferentVersionsProduceDifferentKeys()
9494
// Arrange - Create two service providers with different versions
9595
var servicesV1 = new ServiceCollection();
9696
servicesV1.AddDistributedMemoryCache();
97-
servicesV1.AddMessagePackDistributedCache(options => options.Version = 1);
97+
servicesV1.AddSerializedDistributedCache(options => options.Version = 1);
9898

9999
var servicesV2 = new ServiceCollection();
100100
servicesV2.AddDistributedMemoryCache();
101-
servicesV2.AddMessagePackDistributedCache(options => options.Version = 2);
101+
servicesV2.AddSerializedDistributedCache(options => options.Version = 2);
102102

103103
using var providerV1 = servicesV1.BuildServiceProvider();
104104
using var providerV2 = servicesV2.BuildServiceProvider();
@@ -128,7 +128,7 @@ public void MessagePackCacheKeyIncludesEnvironmentPrefix()
128128
// Arrange
129129
var services = new ServiceCollection();
130130
services.AddDistributedMemoryCache();
131-
services.AddMessagePackDistributedCache(options =>
131+
services.AddSerializedDistributedCache(options =>
132132
{
133133
options.EnvironmentPrefix = "dev";
134134
});
@@ -153,7 +153,7 @@ public void MessagePackCacheKeyIncludesBothVersionAndEnvironment()
153153
// Arrange
154154
var services = new ServiceCollection();
155155
services.AddDistributedMemoryCache();
156-
services.AddMessagePackDistributedCache(options =>
156+
services.AddSerializedDistributedCache(options =>
157157
{
158158
options.Version = 2;
159159
options.EnvironmentPrefix = "prod";
@@ -179,7 +179,7 @@ public void MessagePackCacheKeyWithOptionalKeyAndVersion()
179179
// Arrange
180180
var services = new ServiceCollection();
181181
services.AddDistributedMemoryCache();
182-
services.AddMessagePackDistributedCache(options =>
182+
services.AddSerializedDistributedCache(options =>
183183
{
184184
options.Version = 2;
185185
options.EnvironmentPrefix = "staging";
@@ -212,7 +212,7 @@ public void NullOrEmptyEnvironmentPrefixIsHandledCorrectly(string? environmentPr
212212
// Arrange
213213
var services = new ServiceCollection();
214214
services.AddDistributedMemoryCache();
215-
services.AddMessagePackDistributedCache(options =>
215+
services.AddSerializedDistributedCache(options =>
216216
{
217217
options.EnvironmentPrefix = environmentPrefix;
218218
});

Neolution.Extensions.Caching.UnitTests/TestData/ServiceCollectionTestDataCollection.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using Microsoft.Extensions.DependencyInjection;
66

77
/// <summary>
8-
/// Test data to test different MessagePack cache configurations.
8+
/// Test data to test different serialized cache configurations.
99
/// </summary>
1010
public class ServiceCollectionTestDataCollection : IEnumerable<object[]>
1111
{
@@ -19,11 +19,11 @@ public IEnumerator<object[]> GetEnumerator()
1919
switch (i)
2020
{
2121
case 0:
22-
services.AddDistributedMemoryCache().AddMessagePackDistributedCache();
22+
services.AddDistributedMemoryCache().AddSerializedDistributedCache();
2323
yield return new object[] { services };
2424
break;
2525
case 1:
26-
services.AddDistributedMemoryCache().AddMessagePackDistributedCache(options => { options.DisableCompression = true; });
26+
services.AddDistributedMemoryCache().AddSerializedDistributedCache(options => { options.DisableCompression = true; });
2727
yield return new object[] { services };
2828
break;
2929
}

0 commit comments

Comments
 (0)