SysCacheService.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. using NewLife.Caching.Models;
  7. namespace Admin.NET.Core.Service;
  8. /// <summary>
  9. /// 系统缓存服务 🧩
  10. /// </summary>
  11. [ApiDescriptionSettings(Order = 400)]
  12. public class SysCacheService : IDynamicApiController, ISingleton
  13. {
  14. private static ICacheProvider _cacheProvider;
  15. private readonly CacheOptions _cacheOptions;
  16. public SysCacheService(ICacheProvider cacheProvider, IOptions<CacheOptions> cacheOptions)
  17. {
  18. _cacheProvider = cacheProvider;
  19. _cacheOptions = cacheOptions.Value;
  20. }
  21. /// <summary>
  22. /// 获取缓存键名集合 🔖
  23. /// </summary>
  24. /// <returns></returns>
  25. [DisplayName("获取缓存键名集合")]
  26. public List<string> GetKeyList()
  27. {
  28. return _cacheProvider.Cache == Cache.Default
  29. ? _cacheProvider.Cache.Keys.Where(u => u.StartsWith(_cacheOptions.Prefix)).Select(u => u[_cacheOptions.Prefix.Length..]).OrderBy(u => u).ToList()
  30. : ((FullRedis)_cacheProvider.Cache).Search($"{_cacheOptions.Prefix}*", int.MaxValue).Select(u => u[_cacheOptions.Prefix.Length..]).OrderBy(u => u).ToList();
  31. }
  32. /// <summary>
  33. /// 增加缓存
  34. /// </summary>
  35. /// <param name="key"></param>
  36. /// <param name="value"></param>
  37. /// <returns></returns>
  38. [NonAction]
  39. public bool Set(string key, object value)
  40. {
  41. if (string.IsNullOrWhiteSpace(key)) return false;
  42. return _cacheProvider.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. if (string.IsNullOrWhiteSpace(key)) return false;
  55. return _cacheProvider.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 _cacheProvider.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 _cacheProvider.Cache.Remove($"{_cacheOptions.Prefix}{key}");
  78. }
  79. /// <summary>
  80. /// 清空所有缓存 🔖
  81. /// </summary>
  82. /// <returns></returns>
  83. [HttpDelete]
  84. [DisplayName("清空所有缓存")]
  85. [ApiDescriptionSettings(Name = "Clear")]
  86. public void Clear()
  87. {
  88. // 清除框架的缓存
  89. _cacheProvider.Cache.Clear();
  90. // 当使用Redis时,清除MemoryCache,SqlSugarFilter会用到
  91. var memoryCache = Cache.Default;
  92. if (memoryCache != _cacheProvider.Cache) memoryCache.Clear();
  93. }
  94. /// <summary>
  95. /// 检查缓存是否存在
  96. /// </summary>
  97. /// <param name="key">键</param>
  98. /// <returns></returns>
  99. [NonAction]
  100. public bool ExistKey(string key)
  101. {
  102. return _cacheProvider.Cache.ContainsKey($"{_cacheOptions.Prefix}{key}");
  103. }
  104. /// <summary>
  105. /// 根据键名前缀删除缓存 🔖
  106. /// </summary>
  107. /// <param name="prefixKey">键名前缀</param>
  108. /// <returns></returns>
  109. [ApiDescriptionSettings(Name = "DeleteByPreKey"), HttpPost]
  110. [DisplayName("根据键名前缀删除缓存")]
  111. public int RemoveByPrefixKey(string prefixKey)
  112. {
  113. var delKeys = _cacheProvider.Cache == Cache.Default
  114. ? _cacheProvider.Cache.Keys.Where(u => u.StartsWith($"{_cacheOptions.Prefix}{prefixKey}")).ToArray()
  115. : ((FullRedis)_cacheProvider.Cache).Search($"{_cacheOptions.Prefix}{prefixKey}*", int.MaxValue).ToArray();
  116. return _cacheProvider.Cache.Remove(delKeys);
  117. }
  118. /// <summary>
  119. /// 根据键名前缀获取键名集合 🔖
  120. /// </summary>
  121. /// <param name="prefixKey">键名前缀</param>
  122. /// <returns></returns>
  123. [DisplayName("根据键名前缀获取键名集合")]
  124. public List<string> GetKeysByPrefixKey(string prefixKey)
  125. {
  126. return _cacheProvider.Cache == Cache.Default
  127. ? _cacheProvider.Cache.Keys.Where(u => u.StartsWith($"{_cacheOptions.Prefix}{prefixKey}")).Select(u => u[_cacheOptions.Prefix.Length..]).ToList()
  128. : ((FullRedis)_cacheProvider.Cache).Search($"{_cacheOptions.Prefix}{prefixKey}*", int.MaxValue).Select(u => u[_cacheOptions.Prefix.Length..]).ToList();
  129. }
  130. /// <summary>
  131. /// 获取缓存值 🔖
  132. /// </summary>
  133. /// <param name="key"></param>
  134. /// <returns></returns>
  135. [DisplayName("获取缓存值")]
  136. public object GetValue(string key)
  137. {
  138. // 若Key经过URL编码则进行解码
  139. if (Regex.IsMatch(key, @"%[0-9a-fA-F]{2}"))
  140. key = HttpUtility.UrlDecode(key);
  141. return _cacheProvider.Cache == Cache.Default
  142. ? _cacheProvider.Cache.Get<object>($"{_cacheOptions.Prefix}{key}")
  143. : _cacheProvider.Cache.Get<string>($"{_cacheOptions.Prefix}{key}");
  144. }
  145. /// <summary>
  146. /// 获取或添加缓存(在数据不存在时执行委托请求数据)
  147. /// </summary>
  148. /// <typeparam name="T"></typeparam>
  149. /// <param name="key"></param>
  150. /// <param name="callback"></param>
  151. /// <param name="expire">过期时间,单位秒</param>
  152. /// <returns></returns>
  153. [NonAction]
  154. public T GetOrAdd<T>(string key, Func<string, T> callback, int expire = -1)
  155. {
  156. if (string.IsNullOrWhiteSpace(key)) return default;
  157. return _cacheProvider.Cache.GetOrAdd($"{_cacheOptions.Prefix}{key}", callback, expire);
  158. }
  159. /// <summary>
  160. /// Hash匹配
  161. /// </summary>
  162. /// <typeparam name="T"></typeparam>
  163. /// <param name="key"></param>
  164. /// <returns></returns>
  165. [NonAction]
  166. public RedisHash<string, T> GetHashMap<T>(string key)
  167. {
  168. return _cacheProvider.Cache.GetDictionary<T>(key) as RedisHash<string, T>;
  169. }
  170. /// <summary>
  171. /// 批量添加HASH
  172. /// </summary>
  173. /// <typeparam name="T"></typeparam>
  174. /// <param name="key"></param>
  175. /// <param name="dic"></param>
  176. /// <returns></returns>
  177. [NonAction]
  178. public bool HashSet<T>(string key, Dictionary<string, T> dic)
  179. {
  180. var hash = GetHashMap<T>(key);
  181. return hash.HMSet(dic);
  182. }
  183. /// <summary>
  184. /// 添加一条HASH
  185. /// </summary>
  186. /// <typeparam name="T"></typeparam>
  187. /// <param name="key"></param>
  188. /// <param name="hashKey"></param>
  189. /// <param name="value"></param>
  190. [NonAction]
  191. public void HashAdd<T>(string key, string hashKey, T value)
  192. {
  193. var hash = GetHashMap<T>(key);
  194. hash.Add(hashKey, value);
  195. }
  196. /// <summary>
  197. /// 获取多条HASH
  198. /// </summary>
  199. /// <typeparam name="T"></typeparam>
  200. /// <param name="key"></param>
  201. /// <param name="fields"></param>
  202. /// <returns></returns>
  203. [NonAction]
  204. public List<T> HashGet<T>(string key, params string[] fields)
  205. {
  206. var hash = GetHashMap<T>(key);
  207. var result = hash.HMGet(fields);
  208. return result.ToList();
  209. }
  210. /// <summary>
  211. /// 获取一条HASH
  212. /// </summary>
  213. /// <typeparam name="T"></typeparam>
  214. /// <param name="key"></param>
  215. /// <param name="field"></param>
  216. /// <returns></returns>
  217. [NonAction]
  218. public T HashGetOne<T>(string key, string field)
  219. {
  220. var hash = GetHashMap<T>(key);
  221. var result = hash.HMGet(new string[] { field });
  222. return result[0];
  223. }
  224. /// <summary>
  225. /// 根据KEY获取所有HASH
  226. /// </summary>
  227. /// <typeparam name="T"></typeparam>
  228. /// <param name="key"></param>
  229. /// <returns></returns>
  230. [NonAction]
  231. public IDictionary<string, T> HashGetAll<T>(string key)
  232. {
  233. var hash = GetHashMap<T>(key);
  234. return hash.GetAll();
  235. }
  236. /// <summary>
  237. /// 删除HASH
  238. /// </summary>
  239. /// <typeparam name="T"></typeparam>
  240. /// <param name="key"></param>
  241. /// <param name="fields"></param>
  242. /// <returns></returns>
  243. [NonAction]
  244. public int HashDel<T>(string key, params string[] fields)
  245. {
  246. var hash = GetHashMap<T>(key);
  247. return hash.HDel(fields);
  248. }
  249. /// <summary>
  250. /// 搜索HASH
  251. /// </summary>
  252. /// <typeparam name="T"></typeparam>
  253. /// <param name="key"></param>
  254. /// <param name="searchModel"></param>
  255. /// <returns></returns>
  256. [NonAction]
  257. public List<KeyValuePair<string, T>> HashSearch<T>(string key, SearchModel searchModel)
  258. {
  259. var hash = GetHashMap<T>(key);
  260. return hash.Search(searchModel).ToList();
  261. }
  262. /// <summary>
  263. /// 搜索HASH
  264. /// </summary>
  265. /// <typeparam name="T"></typeparam>
  266. /// <param name="key"></param>
  267. /// <param name="pattern"></param>
  268. /// <param name="count"></param>
  269. /// <returns></returns>
  270. [NonAction]
  271. public List<KeyValuePair<string, T>> HashSearch<T>(string key, string pattern, int count)
  272. {
  273. var hash = GetHashMap<T>(key);
  274. return hash.Search(pattern, count).ToList();
  275. }
  276. }