From 798cb92f50364a2b315786532860f6658597e19b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B7=B3?= <454313500@qq.com> Date: Sun, 18 Feb 2024 15:02:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E7=9B=91=E6=8E=A7=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../YiDistributedCacheKeyNormalizer.cs | 40 ++++++++ .../Consts/LevelConst.cs | 2 +- .../Services/Monitor/MonitorCacheService.cs | 97 +++++++++++++++---- .../Caches/CaptchaPhoneCacheItem.cs | 2 +- .../Caches/UserInfoCacheItem.cs | 2 +- .../Yi.Framework.Rbac.Domain.csproj | 1 + .../YiFrameworkRbacDomainModule.cs | 2 + Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs | 2 + 8 files changed, 125 insertions(+), 23 deletions(-) create mode 100644 Yi.Abp.Net8/framework/Yi.Framework.Caching.FreeRedis/YiDistributedCacheKeyNormalizer.cs diff --git a/Yi.Abp.Net8/framework/Yi.Framework.Caching.FreeRedis/YiDistributedCacheKeyNormalizer.cs b/Yi.Abp.Net8/framework/Yi.Framework.Caching.FreeRedis/YiDistributedCacheKeyNormalizer.cs new file mode 100644 index 00000000..e3e0e22e --- /dev/null +++ b/Yi.Abp.Net8/framework/Yi.Framework.Caching.FreeRedis/YiDistributedCacheKeyNormalizer.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.Options; +using Volo.Abp.Caching; +using Volo.Abp.DependencyInjection; +using Volo.Abp.MultiTenancy; + +namespace Yi.Framework.Caching.FreeRedis +{ + [Dependency(ReplaceServices =true)] + public class YiDistributedCacheKeyNormalizer : IDistributedCacheKeyNormalizer, ITransientDependency + { + protected ICurrentTenant CurrentTenant { get; } + + protected AbpDistributedCacheOptions DistributedCacheOptions { get; } + + public YiDistributedCacheKeyNormalizer( + ICurrentTenant currentTenant, + IOptions distributedCacheOptions) + { + CurrentTenant = currentTenant; + DistributedCacheOptions = distributedCacheOptions.Value; + } + + public virtual string NormalizeKey(DistributedCacheKeyNormalizeArgs args) + { + var normalizedKey = $"{DistributedCacheOptions.KeyPrefix}{args.Key}"; + + //if (!args.IgnoreMultiTenancy && CurrentTenant.Id.HasValue) + //{ + // normalizedKey = $"t:{CurrentTenant.Id.Value},{normalizedKey}"; + //} + + return normalizedKey; + } + } +} diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain.Shared/Consts/LevelConst.cs b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain.Shared/Consts/LevelConst.cs index e62d9afc..b5bb0b81 100644 --- a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain.Shared/Consts/LevelConst.cs +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain.Shared/Consts/LevelConst.cs @@ -8,7 +8,7 @@ namespace Yi.Framework.Bbs.Domain.Shared.Consts { public class LevelConst { - public const string LevelCacheKey=nameof(LevelCacheKey); + public const string LevelCacheKey="Level:All"; public const string Level_Low_Zero = "经验提升等级低于或等于0"; } diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/Monitor/MonitorCacheService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/Monitor/MonitorCacheService.cs index 2862d243..c6adc05f 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/Monitor/MonitorCacheService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/Monitor/MonitorCacheService.cs @@ -1,5 +1,12 @@ -using Microsoft.AspNetCore.Mvc; +using FreeRedis; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Options; +using Microsoft.VisualBasic; +using TencentCloud.Mna.V20210119.Models; using Volo.Abp.Application.Services; +using Volo.Abp.Caching; +using Volo.Abp.DependencyInjection; using Yi.Framework.Rbac.Application.Contracts.Dtos.MonitorCache; using Yi.Framework.Rbac.Application.Contracts.IServices; @@ -7,37 +14,87 @@ namespace Yi.Framework.Rbac.Application.Services.Monitor { public class MonitorCacheService : ApplicationService, IMonitorCacheService { - private static List monitorCacheNames => new List() - { - new MonitorCacheNameGetListOutputDto{ CacheName="Yi:Login",Remark="登录验证码"}, - new MonitorCacheNameGetListOutputDto{ CacheName="Yi:User",Remark="用户信息"} - }; - private Dictionary monitorCacheNamesDic => monitorCacheNames.ToDictionary(x => x.CacheName, x => x.Remark); - //private CSRedisClient _cacheClient; - public MonitorCacheService() - { - //_cacheClient = redisCacheClient.Client; - } - //cacheKey value为空,只要name和备注 + public IAbpLazyServiceProvider LazyServiceProvider { get; set; } - public List GetName() + /// + /// 缓存前缀 + /// + private string CacheKeyPrefix => LazyServiceProvider.LazyGetRequiredService>().Value.KeyPrefix; + + private bool EnableRedisCache { - //固定的 - return monitorCacheNames; + get + { + var redisEnabled = LazyServiceProvider.LazyGetRequiredService()["Redis:IsEnabled"]; + return redisEnabled.IsNullOrEmpty() || bool.Parse(redisEnabled); + } } + /// + /// 使用懒加载防止报错 + /// + private IRedisClient RedisClient => LazyServiceProvider.LazyGetRequiredService(); + + /// + /// 获取所有key并分组 + /// + /// + public List GetName() + { + VerifyRedisCacheEnable(); + var keys = RedisClient.Keys(CacheKeyPrefix + "*"); + var result = GroupedKeys(keys.ToList()); + var output = result.Select(x => new MonitorCacheNameGetListOutputDto { CacheName = x }).ToList(); + return output; + } + + private List GroupedKeys(List keys) + { + HashSet resultSet = new HashSet(); + foreach (string str in keys) + { + string[] parts = str.Split(':'); + + // 如果字符串中包含冒号,则将第一部分和第二部分进行分组 + if (parts.Length >= 2) + { + string group = $"{parts[0]}:{parts[1]}"; + resultSet.Add(group); + } + // 如果字符串中不包含冒号,则直接进行分组 + else + { + resultSet.Add(str); + } + } + return resultSet.ToList(); + + } + + + + private void VerifyRedisCacheEnable() + { + if (!EnableRedisCache) + { + throw new UserFriendlyException("后端程序未使用Redis缓存,无法对Redis进行监控"); + } + + } + [HttpGet("key/{cacaheName}")] public List GetKey(string cacaheName) { - //var output = _cacheClient.Keys($"{cacaheName}:*"); - return new List() { "1233124", "3124", "1231251", "12312412" }; + VerifyRedisCacheEnable(); + var output = RedisClient.Keys($"{cacaheName}:*").Select(x => x.RemovePreFix(cacaheName+ ":")); + return output.ToList(); } //全部不为空 [HttpGet("value/{cacaheName}/{cacaheKey}")] public MonitorCacheGetListOutputDto GetValue(string cacaheName, string cacaheKey) { - //var value = _cacheClient.Get($"{cacaheName}:{cacaheKey}"); - return new MonitorCacheGetListOutputDto() { CacheKey = cacaheKey, CacheName = cacaheName, CacheValue = "ttt", Remark = monitorCacheNamesDic[cacaheName] }; + var value = RedisClient.HGet($"{cacaheName}:{cacaheKey}", "data"); + return new MonitorCacheGetListOutputDto() { CacheKey = cacaheKey, CacheName = cacaheName, CacheValue = value }; } } diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Caches/CaptchaPhoneCacheItem.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Caches/CaptchaPhoneCacheItem.cs index c18432fa..5ec63192 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Caches/CaptchaPhoneCacheItem.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Caches/CaptchaPhoneCacheItem.cs @@ -20,7 +20,7 @@ namespace Yi.Framework.Rbac.Domain.Shared.Caches public override string ToString() { - return $"Yi:Phone:{Phone}"; + return $"Phone:{Phone}"; } } } diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Caches/UserInfoCacheItem.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Caches/UserInfoCacheItem.cs index 6be1ff3a..6bbc113e 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Caches/UserInfoCacheItem.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Caches/UserInfoCacheItem.cs @@ -23,7 +23,7 @@ namespace Yi.Framework.Rbac.Domain.Shared.Caches public override string ToString() { - return $"Yi:User:{UserId}"; + return $"User:{UserId}"; } } } diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Yi.Framework.Rbac.Domain.csproj b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Yi.Framework.Rbac.Domain.csproj index 124133c1..3617a7e8 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Yi.Framework.Rbac.Domain.csproj +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Yi.Framework.Rbac.Domain.csproj @@ -21,6 +21,7 @@ + diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/YiFrameworkRbacDomainModule.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/YiFrameworkRbacDomainModule.cs index 7d185398..a5568c1e 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/YiFrameworkRbacDomainModule.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/YiFrameworkRbacDomainModule.cs @@ -3,6 +3,7 @@ using Volo.Abp.AspNetCore.SignalR; using Volo.Abp.Caching; using Volo.Abp.Domain; using Volo.Abp.Modularity; +using Yi.Framework.Caching.FreeRedis; using Yi.Framework.Mapster; using Yi.Framework.Rbac.Domain.Authorization; using Yi.Framework.Rbac.Domain.Operlog; @@ -13,6 +14,7 @@ namespace Yi.Framework.Rbac.Domain { [DependsOn( typeof(YiFrameworkRbacDomainSharedModule), + typeof(YiFrameworkCachingFreeRedisModule), typeof(AbpAspNetCoreSignalRModule), typeof(AbpDddDomainModule), diff --git a/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs b/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs index 65b0d18c..787600a0 100644 --- a/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs +++ b/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs @@ -87,6 +87,8 @@ namespace Yi.Abp.Web Configure(cacheOptions => { cacheOptions.GlobalCacheEntryOptions.SlidingExpiration =null; + //缓存key前缀 + cacheOptions.KeyPrefix = "Yi:"; });