Redis/NewLife.Redis.Extensions/RedisCacheServiceCollection...

52 lines
1.9 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using NewLife.Caching;
using NewLife.Caching.Services;
namespace NewLife.Redis.Extensions;
/// <summary>
/// Redis分布式缓存扩展
/// </summary>
public static class RedisCacheServiceCollectionExtensions
{
/// <summary>
/// 添加Redis分布式缓存应用内可使用RedisCache/FullRedis/Redis/IDistributedCache/ICache/ICacheProvider
/// </summary>
/// <param name="services"></param>
/// <param name="setupAction"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public static IServiceCollection AddDistributedRedisCache(this IServiceCollection services, Action<RedisOptions> setupAction)
{
if (services == null)
throw new ArgumentNullException(nameof(services));
if (setupAction == null)
throw new ArgumentNullException(nameof(setupAction));
services.AddOptions();
services.Configure(setupAction);
services.AddSingleton(sp => new RedisCache(sp, sp.GetRequiredService<IOptions<RedisOptions>>()));
services.AddSingleton<IDistributedCache>(sp => sp.GetRequiredService<RedisCache>());
services.TryAddSingleton<FullRedis>(sp => sp.GetRequiredService<RedisCache>());
services.TryAddSingleton<ICache>(p => p.GetRequiredService<RedisCache>());
services.TryAddSingleton<Caching.Redis>(p => p.GetRequiredService<RedisCache>());
// 注册Redis缓存服务
services.TryAddSingleton(p =>
{
var redis = p.GetRequiredService<RedisCache>();
var provider = new RedisCacheProvider(p);
if (provider.Cache is not Caching.Redis) provider.Cache = redis;
provider.RedisQueue ??= redis;
return provider;
});
return services;
}
}