SysCacheService.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. return _cache == Cache.Default
  31. ? _cache.Keys.Where(u => u.StartsWith(_cacheOptions.Prefix)).Select(u => u[_cacheOptions.Prefix.Length..]).OrderBy(u => u).ToList()
  32. : ((FullRedis)_cache).Search($"{_cacheOptions.Prefix}*", int.MaxValue).Select(u => u[_cacheOptions.Prefix.Length..]).OrderBy(u => u).ToList();
  33. }
  34. /// <summary>
  35. /// 增加缓存
  36. /// </summary>
  37. /// <param name="key"></param>
  38. /// <param name="value"></param>
  39. /// <returns></returns>
  40. [NonAction]
  41. public bool Set(string key, object value)
  42. {
  43. return _cache.Set($"{_cacheOptions.Prefix}{key}", value);
  44. }
  45. /// <summary>
  46. /// 增加缓存并设置过期时间
  47. /// </summary>
  48. /// <param name="key"></param>
  49. /// <param name="value"></param>
  50. /// <param name="expire"></param>
  51. /// <returns></returns>
  52. [NonAction]
  53. public bool Set(string key, object value, TimeSpan expire)
  54. {
  55. return _cache.Set($"{_cacheOptions.Prefix}{key}", value, expire);
  56. }
  57. /// <summary>
  58. /// 获取缓存
  59. /// </summary>
  60. /// <typeparam name="T"></typeparam>
  61. /// <param name="key"></param>
  62. /// <returns></returns>
  63. [NonAction]
  64. public T Get<T>(string key)
  65. {
  66. return _cache.Get<T>($"{_cacheOptions.Prefix}{key}");
  67. }
  68. /// <summary>
  69. /// 删除缓存
  70. /// </summary>
  71. /// <param name="key"></param>
  72. /// <returns></returns>
  73. [ApiDescriptionSettings(Name = "Delete"), HttpPost]
  74. [DisplayName("删除缓存")]
  75. public int Remove(string key)
  76. {
  77. return _cache.Remove($"{_cacheOptions.Prefix}{key}");
  78. }
  79. /// <summary>
  80. /// 检查缓存是否存在
  81. /// </summary>
  82. /// <param name="key">键</param>
  83. /// <returns></returns>
  84. [NonAction]
  85. public bool ExistKey(string key)
  86. {
  87. return _cache.ContainsKey($"{_cacheOptions.Prefix}{key}");
  88. }
  89. /// <summary>
  90. /// 根据键名前缀删除缓存
  91. /// </summary>
  92. /// <param name="prefixKey">键名前缀</param>
  93. /// <returns></returns>
  94. [ApiDescriptionSettings(Name = "DeleteByPreKey"), HttpPost]
  95. [DisplayName("根据键名前缀删除缓存")]
  96. public int RemoveByPrefixKey(string prefixKey)
  97. {
  98. var delKeys = _cache == Cache.Default
  99. ? _cache.Keys.Where(u => u.StartsWith($"{_cacheOptions.Prefix}{prefixKey}")).ToArray()
  100. : ((FullRedis)_cache).Search($"{_cacheOptions.Prefix}{prefixKey}*", int.MaxValue).ToArray();
  101. return _cache.Remove(delKeys);
  102. }
  103. /// <summary>
  104. /// 根据键名前缀获取键名集合
  105. /// </summary>
  106. /// <param name="prefixKey">键名前缀</param>
  107. /// <returns></returns>
  108. [DisplayName("根据键名前缀获取键名集合")]
  109. public List<string> GetKeysByPrefixKey(string prefixKey)
  110. {
  111. return _cache == Cache.Default
  112. ? _cache.Keys.Where(u => u.StartsWith($"{_cacheOptions.Prefix}{prefixKey}")).Select(u => u[_cacheOptions.Prefix.Length..]).ToList()
  113. : ((FullRedis)_cache).Search($"{_cacheOptions.Prefix}{prefixKey}*", int.MaxValue).Select(u => u[_cacheOptions.Prefix.Length..]).ToList();
  114. }
  115. /// <summary>
  116. /// 获取缓存值
  117. /// </summary>
  118. /// <param name="key"></param>
  119. /// <returns></returns>
  120. [DisplayName("获取缓存值")]
  121. public object GetValue(string key)
  122. {
  123. return _cache == Cache.Default
  124. ? _cache.Get<object>($"{_cacheOptions.Prefix}{key}")
  125. : _cache.Get<string>($"{_cacheOptions.Prefix}{key}");
  126. }
  127. /// <summary>
  128. /// 获取或添加缓存,在数据不存在时执行委托请求数据
  129. /// </summary>
  130. /// <typeparam name="T"></typeparam>
  131. /// <param name="key"></param>
  132. /// <param name="callback"></param>
  133. /// <param name="expire">过期时间,单位秒</param>
  134. /// <returns></returns>
  135. [NonAction]
  136. public T GetOrAdd<T>(string key, Func<string, T> callback, int expire = -1)
  137. {
  138. return _cache.GetOrAdd($"{_cacheOptions.Prefix}{key}", callback, expire);
  139. }
  140. }