Files
Api/DouyinApi.Extensions/ServiceExtensions/CacheSetup.cs
2025-11-04 21:09:16 +08:00

58 lines
2.3 KiB
C#
Raw 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 System.Threading.Tasks;
using DouyinApi.Common;
using DouyinApi.Common.Caches;
using DouyinApi.Common.Caches.Distributed;
using DouyinApi.Common.Caches.Interface;
using DouyinApi.Common.Option;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Serilog;
using StackExchange.Redis;
namespace DouyinApi.Extensions.ServiceExtensions;
public static class CacheSetup
{
/// <summary>
/// 统一注册缓存
/// </summary>
/// <param name="services"></param>
public static void AddCacheSetup(this IServiceCollection services)
{
var cacheOptions = App.GetOptions<RedisOptions>();
if (cacheOptions.Enable)
{
// 配置启动Redis服务虽然可能影响项目启动速度但是不能在运行的时候报错所以是合理的
services.AddSingleton<IConnectionMultiplexer>(sp =>
{
//获取连接字符串
var configuration = ConfigurationOptions.Parse(cacheOptions.ConnectionString, true);
configuration.ResolveDns = true;
return ConnectionMultiplexer.Connect(configuration);
});
services.AddSingleton(p => p.GetService<IConnectionMultiplexer>() as ConnectionMultiplexer);
//使用Redis
services.AddStackExchangeRedisCache(options =>
{
options.ConnectionMultiplexerFactory =
() => Task.FromResult(App.GetService<IConnectionMultiplexer>(false));
if (!cacheOptions.InstanceName.IsNullOrEmpty()) options.InstanceName = cacheOptions.InstanceName;
});
services.AddTransient<IRedisBasketRepository, RedisBasketRepository>();
}
else
{
//使用内存
services.Remove(services.FirstOrDefault(x => x.ServiceType == typeof(IMemoryCache)));
services.AddSingleton<MemoryCacheManager>();
services.AddSingleton<IMemoryCache>(provider => provider.GetService<MemoryCacheManager>());
services.AddOptions();
services.AddSingleton<IDistributedCache, CommonMemoryDistributedCache>();
}
services.AddSingleton<ICaching, Caching>();
}
}