Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
static Microsoft.Extensions.DependencyInjection.StackExchangeRedisCacheServiceCollectionExtensions.AddStackExchangeRedisCache(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> Microsoft.Extensions.DependencyInjection.IServiceCollection!
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.StackExchangeRedis;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Microsoft.Extensions.DependencyInjection;

Expand All @@ -12,6 +13,25 @@ namespace Microsoft.Extensions.DependencyInjection;
/// </summary>
public static class StackExchangeRedisCacheServiceCollectionExtensions
{
/// <summary>
/// Adds Redis distributed caching services to the specified <see cref="IServiceCollection" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection AddStackExchangeRedisCache(this IServiceCollection services)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can redis be used with only default settings? I doesn't look like it. This overload shouldn't exist if settings are required.

{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}

services.AddOptions();

services.TryAdd(ServiceDescriptor.Singleton<IDistributedCache, RedisCache>());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this change from Add to TryAdd? It breaks a test and is a behavioral change


return services;
}

/// <summary>
/// Adds Redis distributed caching services to the specified <see cref="IServiceCollection" />.
/// </summary>
Expand All @@ -31,9 +51,8 @@ public static IServiceCollection AddStackExchangeRedisCache(this IServiceCollect
throw new ArgumentNullException(nameof(setupAction));
}

services.AddOptions();
services.AddStackExchangeRedisCache();
services.Configure(setupAction);
services.Add(ServiceDescriptor.Singleton<IDistributedCache, RedisCache>());

return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,20 @@ public void AddStackExchangeRedisCache_allows_chaining()

Assert.Same(services, services.AddStackExchangeRedisCache(_ => { }));
}

[Fact]
public void AddStackExchangeRedisCache_RegistersRedisCacheWithoutSetupAction()
{
// Arrange
var services = new ServiceCollection();

// Act
services.AddStackExchangeRedisCache();
var distributedCache = services.FirstOrDefault(x => x.ServiceType == typeof(IDistributedCache));

// Assert
Assert.NotNull(distributedCache);
Assert.Equal(typeof(RedisCache), distributedCache.ImplementationType);
Assert.Equal(ServiceLifetime.Singleton, distributedCache.Lifetime);
}
}