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