SysCacheService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. /// <param name="key">要锁定的key</param>
  25. /// <param name="msTimeout">申请锁等待的时间,单位毫秒</param>
  26. /// <param name="msExpire">锁过期时间,超过该时间没有主动是放则自动是放,必须整数秒,单位毫秒</param>
  27. /// <param name="throwOnFailure">失败时是否抛出异常,如不抛出异常,可通过判断返回null得知申请锁失败</param>
  28. /// <returns></returns>
  29. public IDisposable? BeginCacheLock(string key, int msTimeout = 500, int msExpire = 10000, bool throwOnFailure = true)
  30. {
  31. try
  32. {
  33. return _cacheProvider.Cache.AcquireLock(key, msTimeout, msExpire, throwOnFailure);
  34. }
  35. catch
  36. {
  37. return null;
  38. }
  39. }
  40. /// <summary>
  41. /// 获取缓存键名集合 🔖
  42. /// </summary>
  43. /// <returns></returns>
  44. [DisplayName("获取缓存键名集合")]
  45. public List<string> GetKeyList()
  46. {
  47. return _cacheProvider.Cache == Cache.Default
  48. ? _cacheProvider.Cache.Keys.Where(u => u.StartsWith(_cacheOptions.Prefix)).Select(u => u[_cacheOptions.Prefix.Length..]).OrderBy(u => u).ToList()
  49. : ((FullRedis)_cacheProvider.Cache).Search($"{_cacheOptions.Prefix}*", int.MaxValue).Select(u => u[_cacheOptions.Prefix.Length..]).OrderBy(u => u).ToList();
  50. }
  51. /// <summary>
  52. /// 增加缓存
  53. /// </summary>
  54. /// <param name="key"></param>
  55. /// <param name="value"></param>
  56. /// <returns></returns>
  57. [NonAction]
  58. public bool Set(string key, object value)
  59. {
  60. if (string.IsNullOrWhiteSpace(key)) return false;
  61. return _cacheProvider.Cache.Set($"{_cacheOptions.Prefix}{key}", value);
  62. }
  63. /// <summary>
  64. /// 增加缓存并设置过期时间
  65. /// </summary>
  66. /// <param name="key"></param>
  67. /// <param name="value"></param>
  68. /// <param name="expire"></param>
  69. /// <returns></returns>
  70. [NonAction]
  71. public bool Set(string key, object value, TimeSpan expire)
  72. {
  73. if (string.IsNullOrWhiteSpace(key)) return false;
  74. return _cacheProvider.Cache.Set($"{_cacheOptions.Prefix}{key}", value, expire);
  75. }
  76. /// <summary>
  77. /// 获取缓存
  78. /// </summary>
  79. /// <typeparam name="T"></typeparam>
  80. /// <param name="key"></param>
  81. /// <returns></returns>
  82. [NonAction]
  83. public T Get<T>(string key)
  84. {
  85. return _cacheProvider.Cache.Get<T>($"{_cacheOptions.Prefix}{key}");
  86. }
  87. /// <summary>
  88. /// 删除缓存 🔖
  89. /// </summary>
  90. /// <param name="key"></param>
  91. /// <returns></returns>
  92. [ApiDescriptionSettings(Name = "Delete"), HttpPost]
  93. [DisplayName("删除缓存")]
  94. public int Remove(string key)
  95. {
  96. return _cacheProvider.Cache.Remove($"{_cacheOptions.Prefix}{key}");
  97. }
  98. /// <summary>
  99. /// 清空所有缓存 🔖
  100. /// </summary>
  101. /// <returns></returns>
  102. [DisplayName("清空所有缓存")]
  103. [ApiDescriptionSettings(Name = "Clear"), HttpPost]
  104. public void Clear()
  105. {
  106. _cacheProvider.Cache.Clear();
  107. Cache.Default.Clear();
  108. }
  109. /// <summary>
  110. /// 检查缓存是否存在
  111. /// </summary>
  112. /// <param name="key">键</param>
  113. /// <returns></returns>
  114. [NonAction]
  115. public bool ExistKey(string key)
  116. {
  117. return _cacheProvider.Cache.ContainsKey($"{_cacheOptions.Prefix}{key}");
  118. }
  119. /// <summary>
  120. /// 根据键名前缀删除缓存 🔖
  121. /// </summary>
  122. /// <param name="prefixKey">键名前缀</param>
  123. /// <returns></returns>
  124. [ApiDescriptionSettings(Name = "DeleteByPreKey"), HttpPost]
  125. [DisplayName("根据键名前缀删除缓存")]
  126. public int RemoveByPrefixKey(string prefixKey)
  127. {
  128. var delKeys = _cacheProvider.Cache == Cache.Default
  129. ? _cacheProvider.Cache.Keys.Where(u => u.StartsWith($"{_cacheOptions.Prefix}{prefixKey}")).ToArray()
  130. : ((FullRedis)_cacheProvider.Cache).Search($"{_cacheOptions.Prefix}{prefixKey}*", int.MaxValue).ToArray();
  131. return _cacheProvider.Cache.Remove(delKeys);
  132. }
  133. /// <summary>
  134. /// 根据键名前缀获取键名集合 🔖
  135. /// </summary>
  136. /// <param name="prefixKey">键名前缀</param>
  137. /// <returns></returns>
  138. [DisplayName("根据键名前缀获取键名集合")]
  139. public List<string> GetKeysByPrefixKey(string prefixKey)
  140. {
  141. return _cacheProvider.Cache == Cache.Default
  142. ? _cacheProvider.Cache.Keys.Where(u => u.StartsWith($"{_cacheOptions.Prefix}{prefixKey}")).Select(u => u[_cacheOptions.Prefix.Length..]).ToList()
  143. : ((FullRedis)_cacheProvider.Cache).Search($"{_cacheOptions.Prefix}{prefixKey}*", int.MaxValue).Select(u => u[_cacheOptions.Prefix.Length..]).ToList();
  144. }
  145. /// <summary>
  146. /// 获取缓存值 🔖
  147. /// </summary>
  148. /// <param name="key"></param>
  149. /// <returns></returns>
  150. [DisplayName("获取缓存值")]
  151. public object GetValue(string key)
  152. {
  153. // 若Key经过URL编码则进行解码
  154. if (Regex.IsMatch(key, @"%[0-9a-fA-F]{2}"))
  155. key = HttpUtility.UrlDecode(key);
  156. return _cacheProvider.Cache == Cache.Default
  157. ? _cacheProvider.Cache.Get<object>($"{_cacheOptions.Prefix}{key}")
  158. : _cacheProvider.Cache.Get<string>($"{_cacheOptions.Prefix}{key}");
  159. }
  160. /// <summary>
  161. /// 获取或添加缓存(在数据不存在时执行委托请求数据)
  162. /// </summary>
  163. /// <typeparam name="T"></typeparam>
  164. /// <param name="key"></param>
  165. /// <param name="callback"></param>
  166. /// <param name="expire">过期时间,单位秒</param>
  167. /// <returns></returns>
  168. [NonAction]
  169. public T GetOrAdd<T>(string key, Func<string, T> callback, int expire = -1)
  170. {
  171. if (string.IsNullOrWhiteSpace(key)) return default;
  172. return _cacheProvider.Cache.GetOrAdd($"{_cacheOptions.Prefix}{key}", callback, expire);
  173. }
  174. /// <summary>
  175. /// Hash匹配
  176. /// </summary>
  177. /// <typeparam name="T"></typeparam>
  178. /// <param name="key"></param>
  179. /// <returns></returns>
  180. [NonAction]
  181. public RedisHash<string, T> GetHashMap<T>(string key)
  182. {
  183. return _cacheProvider.Cache.GetDictionary<T>(key) as RedisHash<string, T>;
  184. }
  185. /// <summary>
  186. /// 批量添加HASH
  187. /// </summary>
  188. /// <typeparam name="T"></typeparam>
  189. /// <param name="key"></param>
  190. /// <param name="dic"></param>
  191. /// <returns></returns>
  192. [NonAction]
  193. public bool HashSet<T>(string key, Dictionary<string, T> dic)
  194. {
  195. var hash = GetHashMap<T>(key);
  196. return hash.HMSet(dic);
  197. }
  198. /// <summary>
  199. /// 添加一条HASH
  200. /// </summary>
  201. /// <typeparam name="T"></typeparam>
  202. /// <param name="key"></param>
  203. /// <param name="hashKey"></param>
  204. /// <param name="value"></param>
  205. [NonAction]
  206. public void HashAdd<T>(string key, string hashKey, T value)
  207. {
  208. var hash = GetHashMap<T>(key);
  209. hash.Add(hashKey, value);
  210. }
  211. /// <summary>
  212. /// 获取多条HASH
  213. /// </summary>
  214. /// <typeparam name="T"></typeparam>
  215. /// <param name="key"></param>
  216. /// <param name="fields"></param>
  217. /// <returns></returns>
  218. [NonAction]
  219. public List<T> HashGet<T>(string key, params string[] fields)
  220. {
  221. var hash = GetHashMap<T>(key);
  222. var result = hash.HMGet(fields);
  223. return result.ToList();
  224. }
  225. /// <summary>
  226. /// 获取一条HASH
  227. /// </summary>
  228. /// <typeparam name="T"></typeparam>
  229. /// <param name="key"></param>
  230. /// <param name="field"></param>
  231. /// <returns></returns>
  232. [NonAction]
  233. public T HashGetOne<T>(string key, string field)
  234. {
  235. var hash = GetHashMap<T>(key);
  236. var result = hash.HMGet(new string[] { field });
  237. return result[0];
  238. }
  239. /// <summary>
  240. /// 根据KEY获取所有HASH
  241. /// </summary>
  242. /// <typeparam name="T"></typeparam>
  243. /// <param name="key"></param>
  244. /// <returns></returns>
  245. [NonAction]
  246. public IDictionary<string, T> HashGetAll<T>(string key)
  247. {
  248. var hash = GetHashMap<T>(key);
  249. return hash.GetAll();
  250. }
  251. /// <summary>
  252. /// 删除HASH
  253. /// </summary>
  254. /// <typeparam name="T"></typeparam>
  255. /// <param name="key"></param>
  256. /// <param name="fields"></param>
  257. /// <returns></returns>
  258. [NonAction]
  259. public int HashDel<T>(string key, params string[] fields)
  260. {
  261. var hash = GetHashMap<T>(key);
  262. return hash.HDel(fields);
  263. }
  264. /// <summary>
  265. /// 搜索HASH
  266. /// </summary>
  267. /// <typeparam name="T"></typeparam>
  268. /// <param name="key"></param>
  269. /// <param name="searchModel"></param>
  270. /// <returns></returns>
  271. [NonAction]
  272. public List<KeyValuePair<string, T>> HashSearch<T>(string key, SearchModel searchModel)
  273. {
  274. var hash = GetHashMap<T>(key);
  275. return hash.Search(searchModel).ToList();
  276. }
  277. /// <summary>
  278. /// 搜索HASH
  279. /// </summary>
  280. /// <typeparam name="T"></typeparam>
  281. /// <param name="key"></param>
  282. /// <param name="pattern"></param>
  283. /// <param name="count"></param>
  284. /// <returns></returns>
  285. [NonAction]
  286. public List<KeyValuePair<string, T>> HashSearch<T>(string key, string pattern, int count)
  287. {
  288. var hash = GetHashMap<T>(key);
  289. return hash.Search(pattern, count).ToList();
  290. }
  291. }