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
{
///
/// 缓存监控
///
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name)]
public class CacheController : BaseApiController
{
readonly IRedisBasketRepository _redisBasketRepository;
///
/// 构造函数
///
///
public CacheController(IRedisBasketRepository redisBasketRepository)
{
_redisBasketRepository = redisBasketRepository;
}
///
/// 获取缓存详情
///
///
[HttpPost]
public MessageModel> GetList([FromBody] PageQuery 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 pageModel = new PageModel()
{
dataCount = redisKeys.Count,
data = redisKeys.Skip(pageQuery.PageSize * (pageQuery.Page - 1)).Take(pageQuery.PageSize).ToList()
};
return Success(pageModel);
}
///
/// 获取缓存服务列表详情
///
///
[HttpGet]
public MessageModel> GetServerList()
{
var redisKeys = _redisBasketRepository.GetAllKes();
List redisServers = new List();
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 pointkeys = new List();
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);
}
///
/// 获取缓存Key列表详情
///
///
[HttpGet]
public MessageModel> GetKeyList([FromBody] PageQuery 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 pageModel = new PageModel()
{
dataCount = redisKeys.Count,
data = keys.Skip(pageQuery.PageSize * (pageQuery.Page - 1)).Take(pageQuery.PageSize).ToList()
};
return Success(pageModel);
}
///
/// 获取基础信息
///
///
[HttpGet]
public MessageModel GetBaseInfo()
{
return Success(_redisBasketRepository.GetBaseInfo());
}
///
/// 获取缓存值
///
///
///
[HttpGet]
public async Task> GetValue(string key)
{
return Success(await _redisBasketRepository.GetValue(key));
}
///
/// 设置缓存
///
///
///
///
[HttpGet]
public async Task> Add(string key, string value)
{
return Success(await _redisBasketRepository.SetStr(key, value));
}
///
/// 删除缓存
///
///
///
[HttpGet]
public async Task> Del(string key)
{
return Success(await _redisBasketRepository.Remove(key));
}
///
/// 清除所有缓存
///
///
[HttpGet]
public async Task> DelAll()
{
await _redisBasketRepository.Clear();
return Success("");
}
///
/// 获取节点状态
///
///
[HttpGet]
public MessageModel> 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);
}
}
}