live-forum/server/webapi/LiveForum/LiveForum.WebApi/Controllers/CacheController.cs
2026-03-24 11:27:37 +08:00

230 lines
7.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using LiveForum.Code.Base;
using LiveForum.IService.Others;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace LiveForum.WebApi.Controllers
{
/// <summary>
/// 缓存管理相关接口
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
public class CacheController : ControllerBase
{
private readonly ICacheClearService _cacheClearService;
public CacheController(ICacheClearService cacheClearService)
{
_cacheClearService = cacheClearService;
}
/// <summary>
/// 清除所有缓存(支持模式匹配)
/// </summary>
/// <param name="request">请求参数,包含模式(如 cache:api:Config:*</param>
/// <returns></returns>
[HttpPost]
public async Task<BaseResponse<ClearCacheRespDto>> ClearAll([FromBody] ClearAllCacheReq request)
{
try
{
var success = await _cacheClearService.ClearAllCacheAsync(request.Pattern);
return new BaseResponse<ClearCacheRespDto>(new ClearCacheRespDto
{
Success = success,
Message = success ? "清除成功" : "清除失败"
});
}
catch (System.Exception ex)
{
return new BaseResponse<ClearCacheRespDto>(ResponseCode.Error, $"清除缓存失败:{ex.Message}");
}
}
/// <summary>
/// 清除指定Controller的缓存
/// </summary>
/// <param name="request">请求参数包含Controller名称</param>
/// <returns></returns>
[HttpPost]
public async Task<BaseResponse<ClearCacheRespDto>> ClearByController([FromBody] ClearCacheByControllerReq request)
{
try
{
var success = await _cacheClearService.ClearCacheByControllerAsync(request.Controller);
return new BaseResponse<ClearCacheRespDto>(new ClearCacheRespDto
{
Success = success,
Message = success ? "清除成功" : "清除失败"
});
}
catch (System.Exception ex)
{
return new BaseResponse<ClearCacheRespDto>(ResponseCode.Error, $"清除缓存失败:{ex.Message}");
}
}
/// <summary>
/// 清除指定Action的缓存
/// </summary>
/// <param name="request">请求参数包含Controller和Action名称</param>
/// <returns></returns>
[HttpPost]
public async Task<BaseResponse<ClearCacheRespDto>> ClearByAction([FromBody] ClearCacheByActionReq request)
{
try
{
var success = await _cacheClearService.ClearCacheByActionAsync(request.Controller, request.Action);
return new BaseResponse<ClearCacheRespDto>(new ClearCacheRespDto
{
Success = success,
Message = success ? "清除成功" : "清除失败"
});
}
catch (System.Exception ex)
{
return new BaseResponse<ClearCacheRespDto>(ResponseCode.Error, $"清除缓存失败:{ex.Message}");
}
}
/// <summary>
/// 清除指定Key的缓存
/// </summary>
/// <param name="request">请求参数包含缓存Key</param>
/// <returns></returns>
[HttpPost]
public async Task<BaseResponse<ClearCacheRespDto>> ClearByKey([FromBody] ClearCacheByKeyReq request)
{
try
{
var success = await _cacheClearService.ClearCacheByKeyAsync(request.CacheKey);
return new BaseResponse<ClearCacheRespDto>(new ClearCacheRespDto
{
Success = success,
Message = success ? "清除成功" : "清除失败"
});
}
catch (System.Exception ex)
{
return new BaseResponse<ClearCacheRespDto>(ResponseCode.Error, $"清除缓存失败:{ex.Message}");
}
}
/// <summary>
/// 查询缓存Key列表
/// </summary>
/// <param name="pattern">缓存Key模式支持通配符*</param>
/// <returns></returns>
[HttpGet]
public async Task<BaseResponse<GetCacheKeysRespDto>> GetKeys([FromQuery] string pattern = null)
{
try
{
var keys = await _cacheClearService.GetCacheKeysAsync(pattern);
return new BaseResponse<GetCacheKeysRespDto>(new GetCacheKeysRespDto
{
Keys = keys,
Count = keys?.Count ?? 0
});
}
catch (System.Exception ex)
{
return new BaseResponse<GetCacheKeysRespDto>(ResponseCode.Error, $"查询缓存Key失败{ex.Message}");
}
}
}
#region DTO
/// <summary>
/// 清除所有缓存请求参数
/// </summary>
public record ClearAllCacheReq
{
/// <summary>
/// 缓存Key模式支持通配符*),如 cache:api:Config:*
/// </summary>
public string Pattern { get; set; }
}
/// <summary>
/// 清除指定Controller缓存请求参数
/// </summary>
public record ClearCacheByControllerReq
{
/// <summary>
/// Controller名称不含Controller后缀如 "Config"
/// </summary>
public string Controller { get; set; }
}
/// <summary>
/// 清除指定Action缓存请求参数
/// </summary>
public record ClearCacheByActionReq
{
/// <summary>
/// Controller名称不含Controller后缀如 "Config"
/// </summary>
public string Controller { get; set; }
/// <summary>
/// Action名称如 "GetAppConfig"
/// </summary>
public string Action { get; set; }
}
/// <summary>
/// 清除指定Key缓存请求参数
/// </summary>
public record ClearCacheByKeyReq
{
/// <summary>
/// 缓存Key
/// </summary>
public string CacheKey { get; set; }
}
/// <summary>
/// 清除缓存响应
/// </summary>
public record ClearCacheRespDto
{
/// <summary>
/// 是否成功
/// </summary>
public bool Success { get; set; }
/// <summary>
/// 消息
/// </summary>
public string Message { get; set; }
}
/// <summary>
/// 获取缓存Key列表响应
/// </summary>
public record GetCacheKeysRespDto
{
/// <summary>
/// 缓存Key列表
/// </summary>
public List<string> Keys { get; set; } = new List<string>();
/// <summary>
/// Key数量
/// </summary>
public int Count { get; set; }
}
#endregion
}