0
0

CacheService.cs 1.7 KB

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