SysCacheService.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // 麻省理工学院许可证
  2. //
  3. // 版权所有 (c) 2021-2023 zuohuaijun,大名科技(天津)有限公司 联系电话/微信:18020030720 QQ:515096995
  4. //
  5. // 特此免费授予获得本软件的任何人以处理本软件的权利,但须遵守以下条件:在所有副本或重要部分的软件中必须包括上述版权声明和本许可声明。
  6. //
  7. // 软件按“原样”提供,不提供任何形式的明示或暗示的保证,包括但不限于对适销性、适用性和非侵权的保证。
  8. // 在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是因合同、侵权或其他方式引起的,与软件或其使用或其他交易有关。
  9. namespace Admin.NET.Core.Service;
  10. /// <summary>
  11. /// 系统缓存服务
  12. /// </summary>
  13. [ApiDescriptionSettings(Order = 400)]
  14. public class SysCacheService : IDynamicApiController, ISingleton
  15. {
  16. private readonly ICache _cache;
  17. private readonly CacheOptions _cacheOptions;
  18. public SysCacheService(ICache cache, IOptions<CacheOptions> cacheOptions)
  19. {
  20. _cache = cache;
  21. _cacheOptions = cacheOptions.Value;
  22. }
  23. /// <summary>
  24. /// 获取缓存键名集合
  25. /// </summary>
  26. /// <returns></returns>
  27. [DisplayName("获取缓存键名集合")]
  28. public List<string> GetKeyList()
  29. {
  30. // 键名去掉全局缓存前缀
  31. return _cache.Keys.Select(u => u[_cacheOptions.Prefix.Length..]).OrderBy(u => u).ToList();
  32. }
  33. /// <summary>
  34. /// 增加缓存
  35. /// </summary>
  36. /// <param name="key"></param>
  37. /// <param name="value"></param>
  38. /// <returns></returns>
  39. [NonAction]
  40. public bool Set(string key, object value)
  41. {
  42. return _cache.Set($"{_cacheOptions.Prefix}{key}", value);
  43. }
  44. /// <summary>
  45. /// 增加缓存并设置过期时间
  46. /// </summary>
  47. /// <param name="key"></param>
  48. /// <param name="value"></param>
  49. /// <param name="expire"></param>
  50. /// <returns></returns>
  51. [NonAction]
  52. public bool Set(string key, object value, TimeSpan expire)
  53. {
  54. return _cache.Set($"{_cacheOptions.Prefix}{key}", value, expire);
  55. }
  56. /// <summary>
  57. /// 获取缓存
  58. /// </summary>
  59. /// <typeparam name="T"></typeparam>
  60. /// <param name="key"></param>
  61. /// <returns></returns>
  62. [NonAction]
  63. public T Get<T>(string key)
  64. {
  65. return _cache.Get<T>($"{_cacheOptions.Prefix}{key}");
  66. }
  67. /// <summary>
  68. /// 删除缓存
  69. /// </summary>
  70. /// <param name="key"></param>
  71. /// <returns></returns>
  72. [ApiDescriptionSettings(Name = "Delete"), HttpPost]
  73. [DisplayName("删除缓存")]
  74. public int Remove(string key)
  75. {
  76. return _cache.Remove($"{_cacheOptions.Prefix}{key}");
  77. }
  78. /// <summary>
  79. /// 检查缓存是否存在
  80. /// </summary>
  81. /// <param name="key">键</param>
  82. /// <returns></returns>
  83. [NonAction]
  84. public bool ExistKey(string key)
  85. {
  86. return _cache.ContainsKey($"{_cacheOptions.Prefix}{key}");
  87. }
  88. /// <summary>
  89. /// 根据键名前缀删除缓存
  90. /// </summary>
  91. /// <param name="prefixKey">键名前缀</param>
  92. /// <returns></returns>
  93. [ApiDescriptionSettings(Name = "DeleteByPreKey"), HttpPost]
  94. [DisplayName("根据键名前缀删除缓存")]
  95. public int RemoveByPrefixKey(string prefixKey)
  96. {
  97. var delKeys = _cache.Keys.Where(u => u.StartsWith(prefixKey)).ToArray();
  98. if (!delKeys.Any()) return 0;
  99. return _cache.Remove(delKeys);
  100. }
  101. /// <summary>
  102. /// 根据键名前缀获取键名集合
  103. /// </summary>
  104. /// <param name="prefixKey">键名前缀</param>
  105. /// <returns></returns>
  106. [DisplayName("根据键名前缀获取键名集合")]
  107. public List<string> GetKeysByPrefixKey(string prefixKey)
  108. {
  109. return _cache.Keys.Where(u => u.StartsWith(prefixKey)).ToList();
  110. }
  111. /// <summary>
  112. /// 获取缓存值
  113. /// </summary>
  114. /// <param name="key"></param>
  115. /// <returns></returns>
  116. [DisplayName("获取缓存值")]
  117. public object GetValue(string key)
  118. {
  119. return _cache.Get<object>($"{_cacheOptions.Prefix}{key}");
  120. }
  121. /// <summary>
  122. /// 获取或添加缓存,在数据不存在时执行委托请求数据
  123. /// </summary>
  124. /// <typeparam name="T"></typeparam>
  125. /// <param name="key"></param>
  126. /// <param name="callback"></param>
  127. /// <param name="expire">过期时间,单位秒</param>
  128. /// <returns></returns>
  129. [NonAction]
  130. public T GetOrAdd<T>(string key, Func<string, T> callback, int expire = -1)
  131. {
  132. return _cache.GetOrAdd($"{_cacheOptions.Prefix}{key}", callback, expire);
  133. }
  134. }