You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
6.2 KiB
C#

1 year ago
using Admin.Core.Common;
using Admin.Core.Extensions;
using Admin.Core.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Admin.Core.Api
{
/// <summary>
/// 缓存监控
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name)]
public class CacheController : BaseApiController
{
readonly IRedisBasketRepository _redisBasketRepository;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="redisBasketRepository"></param>
public CacheController(IRedisBasketRepository redisBasketRepository)
{
_redisBasketRepository = redisBasketRepository;
}
/// <summary>
/// 获取缓存详情
/// </summary>
/// <returns></returns>
[HttpPost]
public MessageModel<PageModel<RedisKeyView>> GetList([FromBody] PageQuery<RedisKeyView> pageQuery)
{
var redisKeys = _redisBasketRepository.GetAllKes();
if (pageQuery.Query.Server.IsNotEmptyOrNull())
{
redisKeys = redisKeys.FindAll(x => x.Server.Contains(pageQuery.Query.Server));
}
if (pageQuery.Query.Point.IsNotEmptyOrNull())
{
redisKeys = redisKeys.FindAll(x => x.Point.Contains(pageQuery.Query.Point));
}
if (pageQuery.Query.Key.IsNotEmptyOrNull())
{
redisKeys = redisKeys.FindAll(x => x.Key.Contains(pageQuery.Query.Key));
}
PageModel<RedisKeyView> pageModel = new PageModel<RedisKeyView>()
{
dataCount = redisKeys.Count,
data = redisKeys.Skip(pageQuery.PageSize * (pageQuery.Page - 1)).Take(pageQuery.PageSize).ToList()
};
return Success(pageModel);
}
/// <summary>
/// 获取缓存服务列表详情
/// </summary>
/// <returns></returns>
[HttpGet]
public MessageModel<List<RedisServerView>> GetServerList()
{
var redisKeys = _redisBasketRepository.GetAllKes();
List<RedisServerView> redisServers = new List<RedisServerView>();
var servers = redisKeys.Select(x => x.Server).Distinct();
foreach (var item in servers)
{
RedisServerView view = new RedisServerView()
{
Server = item,
Points = redisKeys.FindAll(x => x.Server == item).Select(m => m.Point).Distinct().ToList(),
};
List<Pointkey> pointkeys = new List<Pointkey>();
foreach (var point in view.Points)
{
Pointkey pointkey = new Pointkey()
{
Point = point,
Key = redisKeys.FindAll(x => x.Point == point).Select(m => m.Key).Distinct().ToList(),
};
pointkeys.Add(pointkey);
}
view.Pointkeys = pointkeys;
redisServers.Add(view);
}
return Success(redisServers);
}
/// <summary>
/// 获取缓存Key列表详情
/// </summary>
/// <returns></returns>
[HttpGet]
public MessageModel<PageModel<string>> GetKeyList([FromBody] PageQuery<RedisKeyView> pageQuery)
{
var redisKeys = _redisBasketRepository.GetAllKes();
var keys = redisKeys.Select(x => x.Key).Distinct().ToList();
if (pageQuery.Query.Key.IsNotEmptyOrNull())
{
keys = keys.FindAll(x => x.Contains(pageQuery.Query.Key));
}
PageModel<string> pageModel = new PageModel<string>()
{
dataCount = redisKeys.Count,
data = keys.Skip(pageQuery.PageSize * (pageQuery.Page - 1)).Take(pageQuery.PageSize).ToList()
};
return Success(pageModel);
}
/// <summary>
/// 获取基础信息
/// </summary>
/// <returns></returns>
[HttpGet]
public MessageModel<RedisBaseView> GetBaseInfo()
{
return Success(_redisBasketRepository.GetBaseInfo());
}
/// <summary>
/// 获取缓存值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
[HttpGet]
public async Task<MessageModel<string>> GetValue(string key)
{
return Success<string>(await _redisBasketRepository.GetValue(key));
}
/// <summary>
/// 设置缓存
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
[HttpGet]
public async Task<MessageModel<bool>> Add(string key, string value)
{
return Success(await _redisBasketRepository.SetStr(key, value));
}
/// <summary>
/// 删除缓存
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
[HttpGet]
public async Task<MessageModel<bool>> Del(string key)
{
return Success(await _redisBasketRepository.Remove(key));
}
/// <summary>
/// 清除所有缓存
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<MessageModel<string>> DelAll()
{
await _redisBasketRepository.Clear();
return Success<string>("");
}
/// <summary>
/// 获取节点状态
/// </summary>
/// <returns></returns>
[HttpGet]
public MessageModel<List<PointStatus>> GetPointStatus()
{
var list = _redisBasketRepository.GetPointStatus();
var redisKeys = _redisBasketRepository.GetAllKes();
list.ForEach(item =>
{
item.Server = redisKeys.Find(x => x.Point == item.Point)?.Server;
item.KeyN = redisKeys.FindAll(x => x.Point == item.Point).Count;
});
return Success(list);
}
}
}