SysCacheService.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. namespace Admin.NET.Core.Service;
  2. /// <summary>
  3. /// 系统缓存服务
  4. /// </summary>
  5. [ApiDescriptionSettings(Order = 400)]
  6. public class SysCacheService : IDynamicApiController, ISingleton
  7. {
  8. private readonly ICache _cache;
  9. public SysCacheService(ICache cache)
  10. {
  11. _cache = cache;
  12. }
  13. /// <summary>
  14. /// 获取缓存键名集合
  15. /// </summary>
  16. /// <returns></returns>
  17. [ApiDescriptionSettings(Name = "KeyList")]
  18. [DisplayName("获取缓存键名集合")]
  19. public List<string> GetKeyList()
  20. {
  21. return _cache.Keys.ToList();
  22. }
  23. /// <summary>
  24. /// 增加缓存
  25. /// </summary>
  26. /// <param name="key"></param>
  27. /// <param name="value"></param>
  28. /// <returns></returns>
  29. [ApiDescriptionSettings(false)]
  30. public void Set(string key, object value)
  31. {
  32. _cache.Set(key, value);
  33. }
  34. /// <summary>
  35. /// 增加缓存并设置过期时间
  36. /// </summary>
  37. /// <param name="key"></param>
  38. /// <param name="value"></param>
  39. /// <param name="expire"></param>
  40. /// <returns></returns>
  41. [ApiDescriptionSettings(false)]
  42. public void Set(string key, object value, TimeSpan expire)
  43. {
  44. _cache.Set(key, value, expire);
  45. }
  46. /// <summary>
  47. /// 获取缓存
  48. /// </summary>
  49. /// <typeparam name="T"></typeparam>
  50. /// <param name="key"></param>
  51. /// <returns></returns>
  52. [ApiDescriptionSettings(false)]
  53. public T Get<T>(string key)
  54. {
  55. return _cache.Get<T>(key);
  56. }
  57. /// <summary>
  58. /// 删除缓存
  59. /// </summary>
  60. /// <param name="key"></param>
  61. /// <returns></returns>
  62. [ApiDescriptionSettings(Name = "Delete")]
  63. [DisplayName("删除缓存")]
  64. public void Remove(string key)
  65. {
  66. _cache.Remove(key);
  67. }
  68. /// <summary>
  69. /// 检查缓存是否存在
  70. /// </summary>
  71. /// <param name="key">键</param>
  72. /// <returns></returns>
  73. [ApiDescriptionSettings(false)]
  74. public bool ExistKey(string key)
  75. {
  76. return _cache.ContainsKey(key);
  77. }
  78. /// <summary>
  79. /// 根据键名前缀删除缓存
  80. /// </summary>
  81. /// <param name="prefixKey">键名前缀</param>
  82. /// <returns></returns>
  83. [ApiDescriptionSettings(Name = "DeleteByPreKey")]
  84. [DisplayName("根据键名前缀删除缓存")]
  85. public int RemoveByPrefixKey(string prefixKey)
  86. {
  87. var delKeys = _cache.Keys.Where(u => u.StartsWith(prefixKey)).ToArray();
  88. if (!delKeys.Any()) return 0;
  89. return _cache.Remove(delKeys);
  90. }
  91. /// <summary>
  92. /// 获取缓存值
  93. /// </summary>
  94. /// <param name="key"></param>
  95. /// <returns></returns>
  96. [ApiDescriptionSettings(Name = "Value")]
  97. [DisplayName("获取缓存值")]
  98. public dynamic GetValue(string key)
  99. {
  100. return _cache.Get<dynamic>(key);
  101. }
  102. }