SysCacheService.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 virtual 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. /// <summary>
  140. /// Hash匹配
  141. /// </summary>
  142. /// <typeparam name="T"></typeparam>
  143. /// <param name="key"></param>
  144. /// <returns></returns>
  145. [NonAction]
  146. public RedisHash<string, T> GetHashMap<T>(string key)
  147. {
  148. return _cache.GetDictionary<T>(key) as RedisHash<string, T>;
  149. }
  150. /// <summary>
  151. /// 批量添加HASH
  152. /// </summary>
  153. /// <typeparam name="T"></typeparam>
  154. /// <param name="key"></param>
  155. /// <param name="dic"></param>
  156. /// <returns></returns>
  157. [NonAction]
  158. public bool HashSet<T>(string key, Dictionary<string, T> dic)
  159. {
  160. var hash = GetHashMap<T>(key);
  161. return hash.HMSet(dic);
  162. }
  163. /// <summary>
  164. /// 添加一条HASH
  165. /// </summary>
  166. /// <typeparam name="T"></typeparam>
  167. /// <param name="key"></param>
  168. /// <param name="hashKey"></param>
  169. /// <param name="value"></param>
  170. [NonAction]
  171. public void HashAdd<T>(string key, string hashKey, T value)
  172. {
  173. var hash = GetHashMap<T>(key);
  174. hash.Add(hashKey, value);
  175. }
  176. /// <summary>
  177. /// 获取多条HASH
  178. /// </summary>
  179. /// <typeparam name="T"></typeparam>
  180. /// <param name="key"></param>
  181. /// <param name="fields"></param>
  182. /// <returns></returns>
  183. [NonAction]
  184. public List<T> HashGet<T>(string key, params string[] fields)
  185. {
  186. var hash = GetHashMap<T>(key);
  187. var result = hash.HMGet(fields);
  188. return result.ToList();
  189. }
  190. /// <summary>
  191. /// 获取一条HASH
  192. /// </summary>
  193. /// <typeparam name="T"></typeparam>
  194. /// <param name="key"></param>
  195. /// <param name="field"></param>
  196. /// <returns></returns>
  197. [NonAction]
  198. public T HashGetOne<T>(string key, string field)
  199. {
  200. var hash = GetHashMap<T>(key);
  201. var result = hash.HMGet(new string[] { field });
  202. return result[0];
  203. }
  204. /// <summary>
  205. /// 根据KEY获取所有HASH
  206. /// </summary>
  207. /// <typeparam name="T"></typeparam>
  208. /// <param name="key"></param>
  209. /// <returns></returns>
  210. [NonAction]
  211. public IDictionary<string, T> HashGetAll<T>(string key)
  212. {
  213. var hash = GetHashMap<T>(key);
  214. return hash.GetAll();
  215. }
  216. /// <summary>
  217. /// 删除HASH
  218. /// </summary>
  219. /// <typeparam name="T"></typeparam>
  220. /// <param name="key"></param>
  221. /// <param name="fields"></param>
  222. /// <returns></returns>
  223. [NonAction]
  224. public int HashDel<T>(string key, params string[] fields)
  225. {
  226. var hash = GetHashMap<T>(key);
  227. return hash.HDel(fields);
  228. }
  229. /// <summary>
  230. /// 搜索HASH
  231. /// </summary>
  232. /// <typeparam name="T"></typeparam>
  233. /// <param name="key"></param>
  234. /// <param name="searchModel"></param>
  235. /// <returns></returns>
  236. [NonAction]
  237. public List<KeyValuePair<string, T>> HashSearch<T>(string key, SearchModel searchModel)
  238. {
  239. var hash = GetHashMap<T>(key);
  240. return hash.Search(searchModel).ToList();
  241. }
  242. /// <summary>
  243. /// 搜索HASH
  244. /// </summary>
  245. /// <typeparam name="T"></typeparam>
  246. /// <param name="key"></param>
  247. /// <param name="pattern"></param>
  248. /// <param name="count"></param>
  249. /// <returns></returns>
  250. [NonAction]
  251. public List<KeyValuePair<string, T>> HashSearch<T>(string key, string pattern, int count)
  252. {
  253. var hash = GetHashMap<T>(key);
  254. return hash.Search(pattern, count).ToList();
  255. }
  256. }