CacheService.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using Admin.Core.Common.Cache;
  2. using Admin.Core.Common.Output;
  3. using Microsoft.Extensions.Logging;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Threading.Tasks;
  9. namespace Admin.Core.Service.Admin.Cache
  10. {
  11. public class CacheService : BaseService, ICacheService
  12. {
  13. public CacheService()
  14. {
  15. }
  16. public IResponseOutput List()
  17. {
  18. var list = new List<object>();
  19. var cacheKey = typeof(CacheKey);
  20. var fields = cacheKey.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
  21. foreach (var field in fields)
  22. {
  23. var descriptionAttribute = field.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() as DescriptionAttribute;
  24. list.Add(new
  25. {
  26. field.Name,
  27. Value = field.GetRawConstantValue().ToString(),
  28. descriptionAttribute?.Description
  29. });
  30. }
  31. return ResponseOutput.Ok(list);
  32. }
  33. public async Task<IResponseOutput> ClearAsync(string cacheKey)
  34. {
  35. Logger.LogWarning($"{User.Id}.{User.Name}清除缓存[{cacheKey}]");
  36. await Cache.DelByPatternAsync(cacheKey);
  37. return ResponseOutput.Ok();
  38. }
  39. }
  40. }