SysCacheService.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // 大名科技(天津)有限公司 版权所有
  2. //
  3. // 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动
  6. //
  7. // 任何基于本项目二次开发而产生的一切法律纠纷和责任,均与作者无关
  8. using NewLife.Caching.Models;
  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. if (string.IsNullOrWhiteSpace(key)) return false;
  44. return _cache.Set($"{_cacheOptions.Prefix}{key}", value);
  45. }
  46. /// <summary>
  47. /// 增加缓存并设置过期时间
  48. /// </summary>
  49. /// <param name="key"></param>
  50. /// <param name="value"></param>
  51. /// <param name="expire"></param>
  52. /// <returns></returns>
  53. [NonAction]
  54. public bool Set(string key, object value, TimeSpan expire)
  55. {
  56. if (string.IsNullOrWhiteSpace(key)) return false;
  57. return _cache.Set($"{_cacheOptions.Prefix}{key}", value, expire);
  58. }
  59. /// <summary>
  60. /// 获取缓存
  61. /// </summary>
  62. /// <typeparam name="T"></typeparam>
  63. /// <param name="key"></param>
  64. /// <returns></returns>
  65. [NonAction]
  66. public T Get<T>(string key)
  67. {
  68. return _cache.Get<T>($"{_cacheOptions.Prefix}{key}");
  69. }
  70. /// <summary>
  71. /// 删除缓存 🔖
  72. /// </summary>
  73. /// <param name="key"></param>
  74. /// <returns></returns>
  75. [ApiDescriptionSettings(Name = "Delete"), HttpPost]
  76. [DisplayName("删除缓存")]
  77. public int Remove(string key)
  78. {
  79. return _cache.Remove($"{_cacheOptions.Prefix}{key}");
  80. }
  81. /// <summary>
  82. /// 检查缓存是否存在
  83. /// </summary>
  84. /// <param name="key">键</param>
  85. /// <returns></returns>
  86. [NonAction]
  87. public bool ExistKey(string key)
  88. {
  89. return _cache.ContainsKey($"{_cacheOptions.Prefix}{key}");
  90. }
  91. /// <summary>
  92. /// 根据键名前缀删除缓存 🔖
  93. /// </summary>
  94. /// <param name="prefixKey">键名前缀</param>
  95. /// <returns></returns>
  96. [ApiDescriptionSettings(Name = "DeleteByPreKey"), HttpPost]
  97. [DisplayName("根据键名前缀删除缓存")]
  98. public int RemoveByPrefixKey(string prefixKey)
  99. {
  100. var delKeys = _cache == Cache.Default
  101. ? _cache.Keys.Where(u => u.StartsWith($"{_cacheOptions.Prefix}{prefixKey}")).ToArray()
  102. : ((FullRedis)_cache).Search($"{_cacheOptions.Prefix}{prefixKey}*", int.MaxValue).ToArray();
  103. return _cache.Remove(delKeys);
  104. }
  105. /// <summary>
  106. /// 根据键名前缀获取键名集合 🔖
  107. /// </summary>
  108. /// <param name="prefixKey">键名前缀</param>
  109. /// <returns></returns>
  110. [DisplayName("根据键名前缀获取键名集合")]
  111. public List<string> GetKeysByPrefixKey(string prefixKey)
  112. {
  113. return _cache == Cache.Default
  114. ? _cache.Keys.Where(u => u.StartsWith($"{_cacheOptions.Prefix}{prefixKey}")).Select(u => u[_cacheOptions.Prefix.Length..]).ToList()
  115. : ((FullRedis)_cache).Search($"{_cacheOptions.Prefix}{prefixKey}*", int.MaxValue).Select(u => u[_cacheOptions.Prefix.Length..]).ToList();
  116. }
  117. /// <summary>
  118. /// 获取缓存值 🔖
  119. /// </summary>
  120. /// <param name="key"></param>
  121. /// <returns></returns>
  122. [DisplayName("获取缓存值")]
  123. public object GetValue(string key)
  124. {
  125. return _cache == Cache.Default
  126. ? _cache.Get<object>($"{_cacheOptions.Prefix}{key}")
  127. : _cache.Get<string>($"{_cacheOptions.Prefix}{key}");
  128. }
  129. /// <summary>
  130. /// 获取或添加缓存(在数据不存在时执行委托请求数据)
  131. /// </summary>
  132. /// <typeparam name="T"></typeparam>
  133. /// <param name="key"></param>
  134. /// <param name="callback"></param>
  135. /// <param name="expire">过期时间,单位秒</param>
  136. /// <returns></returns>
  137. [NonAction]
  138. public T GetOrAdd<T>(string key, Func<string, T> callback, int expire = -1)
  139. {
  140. if (string.IsNullOrWhiteSpace(key)) return default;
  141. return _cache.GetOrAdd($"{_cacheOptions.Prefix}{key}", callback, expire);
  142. }
  143. /// <summary>
  144. /// Hash匹配
  145. /// </summary>
  146. /// <typeparam name="T"></typeparam>
  147. /// <param name="key"></param>
  148. /// <returns></returns>
  149. [NonAction]
  150. public RedisHash<string, T> GetHashMap<T>(string key)
  151. {
  152. return _cache.GetDictionary<T>(key) as RedisHash<string, T>;
  153. }
  154. /// <summary>
  155. /// 批量添加HASH
  156. /// </summary>
  157. /// <typeparam name="T"></typeparam>
  158. /// <param name="key"></param>
  159. /// <param name="dic"></param>
  160. /// <returns></returns>
  161. [NonAction]
  162. public bool HashSet<T>(string key, Dictionary<string, T> dic)
  163. {
  164. var hash = GetHashMap<T>(key);
  165. return hash.HMSet(dic);
  166. }
  167. /// <summary>
  168. /// 添加一条HASH
  169. /// </summary>
  170. /// <typeparam name="T"></typeparam>
  171. /// <param name="key"></param>
  172. /// <param name="hashKey"></param>
  173. /// <param name="value"></param>
  174. [NonAction]
  175. public void HashAdd<T>(string key, string hashKey, T value)
  176. {
  177. var hash = GetHashMap<T>(key);
  178. hash.Add(hashKey, value);
  179. }
  180. /// <summary>
  181. /// 获取多条HASH
  182. /// </summary>
  183. /// <typeparam name="T"></typeparam>
  184. /// <param name="key"></param>
  185. /// <param name="fields"></param>
  186. /// <returns></returns>
  187. [NonAction]
  188. public List<T> HashGet<T>(string key, params string[] fields)
  189. {
  190. var hash = GetHashMap<T>(key);
  191. var result = hash.HMGet(fields);
  192. return result.ToList();
  193. }
  194. /// <summary>
  195. /// 获取一条HASH
  196. /// </summary>
  197. /// <typeparam name="T"></typeparam>
  198. /// <param name="key"></param>
  199. /// <param name="field"></param>
  200. /// <returns></returns>
  201. [NonAction]
  202. public T HashGetOne<T>(string key, string field)
  203. {
  204. var hash = GetHashMap<T>(key);
  205. var result = hash.HMGet(new string[] { field });
  206. return result[0];
  207. }
  208. /// <summary>
  209. /// 根据KEY获取所有HASH
  210. /// </summary>
  211. /// <typeparam name="T"></typeparam>
  212. /// <param name="key"></param>
  213. /// <returns></returns>
  214. [NonAction]
  215. public IDictionary<string, T> HashGetAll<T>(string key)
  216. {
  217. var hash = GetHashMap<T>(key);
  218. return hash.GetAll();
  219. }
  220. /// <summary>
  221. /// 删除HASH
  222. /// </summary>
  223. /// <typeparam name="T"></typeparam>
  224. /// <param name="key"></param>
  225. /// <param name="fields"></param>
  226. /// <returns></returns>
  227. [NonAction]
  228. public int HashDel<T>(string key, params string[] fields)
  229. {
  230. var hash = GetHashMap<T>(key);
  231. return hash.HDel(fields);
  232. }
  233. /// <summary>
  234. /// 搜索HASH
  235. /// </summary>
  236. /// <typeparam name="T"></typeparam>
  237. /// <param name="key"></param>
  238. /// <param name="searchModel"></param>
  239. /// <returns></returns>
  240. [NonAction]
  241. public List<KeyValuePair<string, T>> HashSearch<T>(string key, SearchModel searchModel)
  242. {
  243. var hash = GetHashMap<T>(key);
  244. return hash.Search(searchModel).ToList();
  245. }
  246. /// <summary>
  247. /// 搜索HASH
  248. /// </summary>
  249. /// <typeparam name="T"></typeparam>
  250. /// <param name="key"></param>
  251. /// <param name="pattern"></param>
  252. /// <param name="count"></param>
  253. /// <returns></returns>
  254. [NonAction]
  255. public List<KeyValuePair<string, T>> HashSearch<T>(string key, string pattern, int count)
  256. {
  257. var hash = GetHashMap<T>(key);
  258. return hash.Search(pattern, count).ToList();
  259. }
  260. }