feat: 完成缓存监控模块
This commit is contained in:
@@ -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<AbpDistributedCacheOptions> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
|
||||
@@ -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<MonitorCacheNameGetListOutputDto> monitorCacheNames => new List<MonitorCacheNameGetListOutputDto>()
|
||||
{
|
||||
new MonitorCacheNameGetListOutputDto{ CacheName="Yi:Login",Remark="登录验证码"},
|
||||
new MonitorCacheNameGetListOutputDto{ CacheName="Yi:User",Remark="用户信息"}
|
||||
};
|
||||
private Dictionary<string, string> 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<MonitorCacheNameGetListOutputDto> GetName()
|
||||
/// <summary>
|
||||
/// 缓存前缀
|
||||
/// </summary>
|
||||
private string CacheKeyPrefix => LazyServiceProvider.LazyGetRequiredService<IOptions<AbpDistributedCacheOptions>>().Value.KeyPrefix;
|
||||
|
||||
private bool EnableRedisCache
|
||||
{
|
||||
//固定的
|
||||
return monitorCacheNames;
|
||||
get
|
||||
{
|
||||
var redisEnabled = LazyServiceProvider.LazyGetRequiredService<IConfiguration>()["Redis:IsEnabled"];
|
||||
return redisEnabled.IsNullOrEmpty() || bool.Parse(redisEnabled);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 使用懒加载防止报错
|
||||
/// </summary>
|
||||
private IRedisClient RedisClient => LazyServiceProvider.LazyGetRequiredService<IRedisClient>();
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有key并分组
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public List<MonitorCacheNameGetListOutputDto> 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<string> GroupedKeys(List<string> keys)
|
||||
{
|
||||
HashSet<string> resultSet = new HashSet<string>();
|
||||
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<string> GetKey(string cacaheName)
|
||||
{
|
||||
//var output = _cacheClient.Keys($"{cacaheName}:*");
|
||||
return new List<string>() { "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 };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace Yi.Framework.Rbac.Domain.Shared.Caches
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Yi:Phone:{Phone}";
|
||||
return $"Phone:{Phone}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace Yi.Framework.Rbac.Domain.Shared.Caches
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Yi:User:{UserId}";
|
||||
return $"User:{UserId}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\framework\Yi.Framework.Caching.FreeRedis\Yi.Framework.Caching.FreeRedis.csproj" />
|
||||
<ProjectReference Include="..\..\..\framework\Yi.Framework.SqlSugarCore.Abstractions\Yi.Framework.SqlSugarCore.Abstractions.csproj" />
|
||||
<ProjectReference Include="..\Yi.Framework.Rbac.Domain.Shared\Yi.Framework.Rbac.Domain.Shared.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -87,6 +87,8 @@ namespace Yi.Abp.Web
|
||||
Configure<AbpDistributedCacheOptions>(cacheOptions =>
|
||||
{
|
||||
cacheOptions.GlobalCacheEntryOptions.SlidingExpiration =null;
|
||||
//缓存key前缀
|
||||
cacheOptions.KeyPrefix = "Yi:";
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user