Copy public static void AddRENCaching<TICacheService, TCacheService>(this IServiceCollection services, CacheType cacheType, Func<IServiceProvider, IConnectionMultiplexer>? implementationFactory = null, RedisMultiplexerLifetime? multiplexerLifetime = null)
where TICacheService : class, IRENCacheService
where TCacheService : class, TICacheService
{
switch (cacheType)
{
case CacheType.InMemory:
AddInMemoryRENCache<TICacheService, TCacheService>(services);
break;
case CacheType.Redis:
AddRedisRENCache<TICacheService, TCacheService>(services, implementationFactory, multiplexerLifetime.Value);
break;
default:
throw new ArgumentOutOfRangeException(nameof(cacheType), cacheType, null);
}
}
private static void AddRedisRENCache<TICacheService, TCacheService>(this IServiceCollection services, Func<IServiceProvider, IConnectionMultiplexer> implementationFactory, RedisMultiplexerLifetime multiplexerLifetime)
where TICacheService : class, IRENCacheService
where TCacheService : class, TICacheService
{
services.GetBaseRedisInjectImplementations(implementationFactory, multiplexerLifetime);
services.RegisterRENCacheServices<TICacheService, TCacheService>();
}
private static void GetBaseRedisInjectImplementations(this IServiceCollection services, Func<IServiceProvider, IConnectionMultiplexer> implementationFactory, RedisMultiplexerLifetime multiplexerRuntime)
{
if (implementationFactory == null)
throw new ArgumentNullException(nameof(implementationFactory));
if (multiplexerRuntime == RedisMultiplexerLifetime.Singleton)
services.AddSingleton<IConnectionMultiplexer>(implementationFactory);
else if (multiplexerRuntime == RedisMultiplexerLifetime.Scoped)
services.AddScoped<IConnectionMultiplexer>(implementationFactory);
else if (multiplexerRuntime == RedisMultiplexerLifetime.Transient)
services.AddTransient<IConnectionMultiplexer>(implementationFactory);
else
throw new ArgumentOutOfRangeException(nameof(multiplexerRuntime), multiplexerRuntime, null);
}
private static IServiceCollection RegisterRENCacheServices<TCacheService>(this IServiceCollection services)
where TCacheService : IRENCacheService
{
services.AddScoped(typeof(IRENCacheService), typeof(TCacheService));
return services;
}