SysCacheService.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
  2. //
  3. // 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
  4. using NewLife.Caching.Models;
  5. namespace Admin.NET.Core.Service;
  6. /// <summary>
  7. /// 系统缓存服务
  8. /// </summary>
  9. [ApiDescriptionSettings(Order = 400)]
  10. public class SysCacheService : IDynamicApiController, ISingleton
  11. {
  12. private readonly ICache _cache;
  13. private readonly CacheOptions _cacheOptions;
  14. public SysCacheService(ICache cache, IOptions<CacheOptions> cacheOptions)
  15. {
  16. _cache = cache;
  17. _cacheOptions = cacheOptions.Value;
  18. }
  19. /// <summary>
  20. /// 获取缓存键名集合
  21. /// </summary>
  22. /// <returns></returns>
  23. [DisplayName("获取缓存键名集合")]
  24. public List<string> GetKeyList()
  25. {
  26. return _cache == Cache.Default
  27. ? _cache.Keys.Where(u => u.StartsWith(_cacheOptions.Prefix)).Select(u => u[_cacheOptions.Prefix.Length..]).OrderBy(u => u).ToList()
  28. : ((FullRedis)_cache).Search($"{_cacheOptions.Prefix}*", int.MaxValue).Select(u => u[_cacheOptions.Prefix.Length..]).OrderBy(u => u).ToList();
  29. }
  30. /// <summary>
  31. /// 增加缓存
  32. /// </summary>
  33. /// <param name="key"></param>
  34. /// <param name="value"></param>
  35. /// <returns></returns>
  36. [NonAction]
  37. public bool Set(string key, object value)
  38. {
  39. if (string.IsNullOrWhiteSpace(key)) return false;
  40. return _cache.Set($"{_cacheOptions.Prefix}{key}", value);
  41. }
  42. /// <summary>
  43. /// 增加缓存并设置过期时间
  44. /// </summary>
  45. /// <param name="key"></param>
  46. /// <param name="value"></param>
  47. /// <param name="expire"></param>
  48. /// <returns></returns>
  49. [NonAction]
  50. public bool Set(string key, object value, TimeSpan expire)
  51. {
  52. if (string.IsNullOrWhiteSpace(key)) return false;
  53. return _cache.Set($"{_cacheOptions.Prefix}{key}", value, expire);
  54. }
  55. /// <summary>
  56. /// 获取缓存
  57. /// </summary>
  58. /// <typeparam name="T"></typeparam>
  59. /// <param name="key"></param>
  60. /// <returns></returns>
  61. [NonAction]
  62. public T Get<T>(string key)
  63. {
  64. return _cache.Get<T>($"{_cacheOptions.Prefix}{key}");
  65. }
  66. /// <summary>
  67. /// 删除缓存
  68. /// </summary>
  69. /// <param name="key"></param>
  70. /// <returns></returns>
  71. [ApiDescriptionSettings(Name = "Delete"), HttpPost]
  72. [DisplayName("删除缓存")]
  73. public int Remove(string key)
  74. {
  75. return _cache.Remove($"{_cacheOptions.Prefix}{key}");
  76. }
  77. /// <summary>
  78. /// 检查缓存是否存在
  79. /// </summary>
  80. /// <param name="key">键</param>
  81. /// <returns></returns>
  82. [NonAction]
  83. public bool ExistKey(string key)
  84. {
  85. return _cache.ContainsKey($"{_cacheOptions.Prefix}{key}");
  86. }
  87. /// <summary>
  88. /// 根据键名前缀删除缓存
  89. /// </summary>
  90. /// <param name="prefixKey">键名前缀</param>
  91. /// <returns></returns>
  92. [ApiDescriptionSettings(Name = "DeleteByPreKey"), HttpPost]
  93. [DisplayName("根据键名前缀删除缓存")]
  94. public int RemoveByPrefixKey(string prefixKey)
  95. {
  96. var delKeys = _cache == Cache.Default
  97. ? _cache.Keys.Where(u => u.StartsWith($"{_cacheOptions.Prefix}{prefixKey}")).ToArray()
  98. : ((FullRedis)_cache).Search($"{_cacheOptions.Prefix}{prefixKey}*", int.MaxValue).ToArray();
  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 == Cache.Default
  110. ? _cache.Keys.Where(u => u.StartsWith($"{_cacheOptions.Prefix}{prefixKey}")).Select(u => u[_cacheOptions.Prefix.Length..]).ToList()
  111. : ((FullRedis)_cache).Search($"{_cacheOptions.Prefix}{prefixKey}*", int.MaxValue).Select(u => u[_cacheOptions.Prefix.Length..]).ToList();
  112. }
  113. /// <summary>
  114. /// 获取缓存值
  115. /// </summary>
  116. /// <param name="key"></param>
  117. /// <returns></returns>
  118. [DisplayName("获取缓存值")]
  119. public object GetValue(string key)
  120. {
  121. return _cache == Cache.Default
  122. ? _cache.Get<object>($"{_cacheOptions.Prefix}{key}")
  123. : _cache.Get<string>($"{_cacheOptions.Prefix}{key}");
  124. }
  125. /// <summary>
  126. /// 获取或添加缓存,在数据不存在时执行委托请求数据
  127. /// </summary>
  128. /// <typeparam name="T"></typeparam>
  129. /// <param name="key"></param>
  130. /// <param name="callback"></param>
  131. /// <param name="expire">过期时间,单位秒</param>
  132. /// <returns></returns>
  133. [NonAction]
  134. public T GetOrAdd<T>(string key, Func<string, T> callback, int expire = -1)
  135. {
  136. if (string.IsNullOrWhiteSpace(key)) return default;
  137. return _cache.GetOrAdd($"{_cacheOptions.Prefix}{key}", callback, expire);
  138. }
  139. [NonAction]
  140. public RedisHash<string, T> GetHashMap<T>(string key)
  141. {
  142. return _cache.GetDictionary<T>(key) as RedisHash<string, T>;
  143. }
  144. /// <summary>
  145. /// 批量添加HASH
  146. /// </summary>
  147. /// <typeparam name="T"></typeparam>
  148. /// <param name="key"></param>
  149. /// <param name="dic"></param>
  150. /// <returns></returns>
  151. [NonAction]
  152. public bool HashSet<T>(string key, Dictionary<string, T> dic)
  153. {
  154. var hash = GetHashMap<T>(key);
  155. return hash.HMSet(dic);
  156. }
  157. /// <summary>
  158. /// 添加一条HASH
  159. /// </summary>
  160. /// <typeparam name="T"></typeparam>
  161. /// <param name="key"></param>
  162. /// <param name="hashKey"></param>
  163. /// <param name="value"></param>
  164. [NonAction]
  165. public void HashAdd<T>(string key, string hashKey, T value)
  166. {
  167. var hash = GetHashMap<T>(key);
  168. hash.Add(hashKey, value);
  169. }
  170. /// <summary>
  171. /// 获取多条HASH
  172. /// </summary>
  173. /// <typeparam name="T"></typeparam>
  174. /// <param name="key"></param>
  175. /// <param name="fields"></param>
  176. /// <returns></returns>
  177. [NonAction]
  178. public List<T> HashGet<T>(string key, params string[] fields)
  179. {
  180. var hash = GetHashMap<T>(key);
  181. var result = hash.HMGet(fields);
  182. return result.ToList();
  183. }
  184. /// <summary>
  185. /// 获取一条HASH
  186. /// </summary>
  187. /// <typeparam name="T"></typeparam>
  188. /// <param name="key"></param>
  189. /// <param name="field"></param>
  190. /// <returns></returns>
  191. [NonAction]
  192. public T HashGetOne<T>(string key, string field)
  193. {
  194. var hash = GetHashMap<T>(key);
  195. var result = hash.HMGet(new string[] { field });
  196. return result[0];
  197. }
  198. /// <summary>
  199. /// 根据KEY获取所有HASH
  200. /// </summary>
  201. /// <typeparam name="T"></typeparam>
  202. /// <param name="key"></param>
  203. /// <returns></returns>
  204. [NonAction]
  205. public IDictionary<string, T> HashGetAll<T>(string key)
  206. {
  207. var hash = GetHashMap<T>(key);
  208. return hash.GetAll();
  209. }
  210. /// <summary>
  211. /// 删除HASH
  212. /// </summary>
  213. /// <typeparam name="T"></typeparam>
  214. /// <param name="key"></param>
  215. /// <param name="fields"></param>
  216. /// <returns></returns>
  217. [NonAction]
  218. public int HashDel<T>(string key, params string[] fields)
  219. {
  220. var hash = GetHashMap<T>(key);
  221. return hash.HDel(fields);
  222. }
  223. /// <summary>
  224. /// 搜索HASH
  225. /// </summary>
  226. /// <typeparam name="T"></typeparam>
  227. /// <param name="key"></param>
  228. /// <param name="searchModel"></param>
  229. /// <returns></returns>
  230. [NonAction]
  231. public List<KeyValuePair<string, T>> HashSearch<T>(string key, SearchModel searchModel)
  232. {
  233. var hash = GetHashMap<T>(key);
  234. return hash.Search(searchModel).ToList();
  235. }
  236. /// <summary>
  237. /// 搜索HASH
  238. /// </summary>
  239. /// <typeparam name="T"></typeparam>
  240. /// <param name="key"></param>
  241. /// <param name="pattern"></param>
  242. /// <param name="count"></param>
  243. /// <returns></returns>
  244. [NonAction]
  245. public List<KeyValuePair<string, T>> HashSearch<T>(string key, string pattern, int count)
  246. {
  247. var hash = GetHashMap<T>(key);
  248. return hash.Search(pattern, count).ToList();
  249. }
  250. }