CacheService.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. private readonly ICache _cache;
  14. private readonly ILogger<CacheService> _logger;
  15. public CacheService(ICache cache, ILogger<CacheService> logger)
  16. {
  17. _cache = cache;
  18. _logger = logger;
  19. }
  20. public IResponseOutput List()
  21. {
  22. var list = new List<object>();
  23. var cacheKey = typeof(CacheKey);
  24. var fields = cacheKey.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
  25. foreach (var field in fields)
  26. {
  27. var descriptionAttribute = field.GetCustomAttributes(typeof(DescriptionAttribute),false).FirstOrDefault() as DescriptionAttribute;
  28. list.Add(new
  29. {
  30. field.Name,
  31. Value = field.GetRawConstantValue().ToString(),
  32. descriptionAttribute?.Description
  33. });
  34. }
  35. return ResponseOutput.Ok(list);
  36. }
  37. public async Task<IResponseOutput> ClearAsync(string cacheKey)
  38. {
  39. _logger.LogWarning($"{User.Id}.{User.Name}清除缓存[{cacheKey}]");
  40. await _cache.DelByPatternAsync(cacheKey);
  41. return ResponseOutput.Ok();
  42. }
  43. }
  44. }