CacheService.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using Microsoft.Extensions.Logging;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Threading.Tasks;
  7. using ZhonTai.Common.Domain.Dto;
  8. using ZhonTai.Plate.Admin.Service.Contracts;
  9. using ZhonTai.Tools.DynamicApi;
  10. using ZhonTai.Tools.DynamicApi.Attributes;
  11. namespace ZhonTai.Plate.Admin.Service.Cache
  12. {
  13. /// <summary>
  14. /// 缓存服务
  15. /// </summary>
  16. [DynamicApi(Area = "admin")]
  17. public class CacheService : BaseService, ICacheService, IDynamicApi
  18. {
  19. public CacheService()
  20. {
  21. }
  22. /// <summary>
  23. /// 查询列表
  24. /// </summary>
  25. /// <returns></returns>
  26. public IResultOutput GetList()
  27. {
  28. var list = new List<object>();
  29. var cacheKey = typeof(CacheKey);
  30. var fields = cacheKey.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
  31. foreach (var field in fields)
  32. {
  33. var descriptionAttribute = field.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() as DescriptionAttribute;
  34. list.Add(new
  35. {
  36. field.Name,
  37. Value = field.GetRawConstantValue().ToString(),
  38. descriptionAttribute?.Description
  39. });
  40. }
  41. return ResultOutput.Ok(list);
  42. }
  43. /// <summary>
  44. /// 清除缓存
  45. /// </summary>
  46. /// <param name="cacheKey">缓存键</param>
  47. /// <returns></returns>
  48. public async Task<IResultOutput> ClearAsync(string cacheKey)
  49. {
  50. Logger.LogWarning($"{User.Id}.{User.Name}清除缓存[{cacheKey}]");
  51. await Cache.DelByPatternAsync(cacheKey);
  52. return ResultOutput.Ok();
  53. }
  54. }
  55. }