refactor: ai+人工重构优化 framework
This commit is contained in:
@@ -10,28 +10,43 @@ using Volo.Abp.MultiTenancy;
|
||||
|
||||
namespace Yi.Framework.Caching.FreeRedis
|
||||
{
|
||||
[Dependency(ReplaceServices =true)]
|
||||
/// <summary>
|
||||
/// 缓存键标准化处理器
|
||||
/// 用于处理缓存键的格式化和多租户支持
|
||||
/// </summary>
|
||||
[Dependency(ReplaceServices = true)]
|
||||
public class YiDistributedCacheKeyNormalizer : IDistributedCacheKeyNormalizer, ITransientDependency
|
||||
{
|
||||
protected ICurrentTenant CurrentTenant { get; }
|
||||
|
||||
protected AbpDistributedCacheOptions DistributedCacheOptions { get; }
|
||||
private readonly ICurrentTenant _currentTenant;
|
||||
private readonly AbpDistributedCacheOptions _distributedCacheOptions;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="currentTenant">当前租户服务</param>
|
||||
/// <param name="distributedCacheOptions">分布式缓存配置选项</param>
|
||||
public YiDistributedCacheKeyNormalizer(
|
||||
ICurrentTenant currentTenant,
|
||||
IOptions<AbpDistributedCacheOptions> distributedCacheOptions)
|
||||
{
|
||||
CurrentTenant = currentTenant;
|
||||
DistributedCacheOptions = distributedCacheOptions.Value;
|
||||
_currentTenant = currentTenant;
|
||||
_distributedCacheOptions = distributedCacheOptions.Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 标准化缓存键
|
||||
/// </summary>
|
||||
/// <param name="args">缓存键标准化参数</param>
|
||||
/// <returns>标准化后的缓存键</returns>
|
||||
public virtual string NormalizeKey(DistributedCacheKeyNormalizeArgs args)
|
||||
{
|
||||
var normalizedKey = $"{DistributedCacheOptions.KeyPrefix}{args.Key}";
|
||||
// 添加全局缓存前缀
|
||||
var normalizedKey = $"{_distributedCacheOptions.KeyPrefix}{args.Key}";
|
||||
|
||||
//if (!args.IgnoreMultiTenancy && CurrentTenant.Id.HasValue)
|
||||
//todo 多租户支持已注释,如需启用取消注释即可
|
||||
//if (!args.IgnoreMultiTenancy && _currentTenant.Id.HasValue)
|
||||
//{
|
||||
// normalizedKey = $"t:{CurrentTenant.Id.Value},{normalizedKey}";
|
||||
// normalizedKey = $"t:{_currentTenant.Id.Value},{normalizedKey}";
|
||||
//}
|
||||
|
||||
return normalizedKey;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using FreeRedis;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Volo.Abp.Caching;
|
||||
@@ -7,26 +8,57 @@ using Volo.Abp.Caching;
|
||||
namespace Yi.Framework.Caching.FreeRedis
|
||||
{
|
||||
/// <summary>
|
||||
/// 此模块得益于FreeRedis作者支持IDistributedCache,使用湿滑
|
||||
/// FreeRedis缓存模块
|
||||
/// 提供基于FreeRedis的分布式缓存实现
|
||||
/// </summary>
|
||||
[DependsOn(typeof(AbpCachingModule))]
|
||||
public class YiFrameworkCachingFreeRedisModule : AbpModule
|
||||
{
|
||||
private const string RedisEnabledKey = "Redis:IsEnabled";
|
||||
private const string RedisConfigurationKey = "Redis:Configuration";
|
||||
|
||||
/// <summary>
|
||||
/// 配置服务
|
||||
/// </summary>
|
||||
/// <param name="context">服务配置上下文</param>
|
||||
public override void ConfigureServices(ServiceConfigurationContext context)
|
||||
{
|
||||
|
||||
var configuration = context.Services.GetConfiguration();
|
||||
|
||||
var redisEnabled = configuration["Redis:IsEnabled"];
|
||||
if (redisEnabled.IsNullOrEmpty() || bool.Parse(redisEnabled))
|
||||
// 检查Redis是否启用
|
||||
if (!IsRedisEnabled(configuration))
|
||||
{
|
||||
var redisConfiguration = configuration["Redis:Configuration"];
|
||||
RedisClient redisClient = new RedisClient(redisConfiguration);
|
||||
|
||||
context.Services.AddSingleton<IRedisClient>(redisClient);
|
||||
context.Services.Replace(ServiceDescriptor.Singleton<IDistributedCache>(new
|
||||
DistributedCache(redisClient)));
|
||||
return;
|
||||
}
|
||||
|
||||
// 注册Redis服务
|
||||
RegisterRedisServices(context, configuration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查Redis是否启用
|
||||
/// </summary>
|
||||
/// <param name="configuration">配置</param>
|
||||
/// <returns>是否启用Redis</returns>
|
||||
private static bool IsRedisEnabled(IConfiguration configuration)
|
||||
{
|
||||
var redisEnabled = configuration[RedisEnabledKey];
|
||||
return redisEnabled.IsNullOrEmpty() || bool.Parse(redisEnabled);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册Redis相关服务
|
||||
/// </summary>
|
||||
/// <param name="context">服务配置上下文</param>
|
||||
/// <param name="configuration">配置</param>
|
||||
private static void RegisterRedisServices(ServiceConfigurationContext context, IConfiguration configuration)
|
||||
{
|
||||
var redisConfiguration = configuration[RedisConfigurationKey];
|
||||
var redisClient = new RedisClient(redisConfiguration);
|
||||
|
||||
context.Services.AddSingleton<IRedisClient>(redisClient);
|
||||
context.Services.Replace(ServiceDescriptor.Singleton<IDistributedCache>(
|
||||
new DistributedCache(redisClient)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user