feat: 完成缓存监控模块

This commit is contained in:
陈淳
2024-02-18 15:02:23 +08:00
parent 9477ca0373
commit 798cb92f50
8 changed files with 125 additions and 23 deletions

View File

@@ -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 };
}
}