SysCacheService.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. using Newtonsoft.Json;
  7. namespace Admin.NET.Core.Service;
  8. /// <summary>
  9. /// 系统缓存服务 🧩
  10. /// </summary>
  11. [ApiDescriptionSettings(Order = 400, Description = "系统缓存")]
  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. [DisplayName("申请分布式锁")]
  30. public IDisposable? BeginCacheLock(string key, int msTimeout = 500, int msExpire = 10000, bool throwOnFailure = true)
  31. {
  32. try
  33. {
  34. return _cacheProvider.Cache.AcquireLock(key, msTimeout, msExpire, throwOnFailure);
  35. }
  36. catch
  37. {
  38. return null;
  39. }
  40. }
  41. /// <summary>
  42. /// 获取缓存键名集合 🔖
  43. /// </summary>
  44. /// <returns></returns>
  45. [DisplayName("获取缓存键名集合")]
  46. public List<string> GetKeyList()
  47. {
  48. return _cacheProvider.Cache == Cache.Default
  49. ? [.. _cacheProvider.Cache.Keys.Where(u => u.StartsWith(_cacheOptions.Prefix)).Select(u => u[_cacheOptions.Prefix.Length..]).OrderBy(u => u)]
  50. : [.. ((FullRedis)_cacheProvider.Cache).Search($"{_cacheOptions.Prefix}*", int.MaxValue).Select(u => u[_cacheOptions.Prefix.Length..]).OrderBy(u => u)];
  51. }
  52. /// <summary>
  53. /// 增加缓存
  54. /// </summary>
  55. /// <param name="key"></param>
  56. /// <param name="value"></param>
  57. /// <returns></returns>
  58. [NonAction]
  59. public bool Set(string key, object value)
  60. {
  61. return !string.IsNullOrWhiteSpace(key) && _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. return !string.IsNullOrWhiteSpace(key) && _cacheProvider.Cache.Set($"{_cacheOptions.Prefix}{key}", value, expire);
  74. }
  75. public async Task<TR> AdGetAsync<TR>(String cacheName, Func<Task<TR>> del, TimeSpan? expiry = default) where TR : class
  76. {
  77. return await AdGetAsync<TR>(cacheName, del, [], expiry);
  78. }
  79. public async Task<TR> AdGetAsync<TR, T1>(String cacheName, Func<T1, Task<TR>> del, T1 t1, TimeSpan? expiry = default) where TR : class
  80. {
  81. return await AdGetAsync<TR>(cacheName, del, [t1], expiry);
  82. }
  83. public async Task<TR> AdGetAsync<TR, T1, T2>(String cacheName, Func<T1, T2, Task<TR>> del, T1 t1, T2 t2, TimeSpan? expiry = default) where TR : class
  84. {
  85. return await AdGetAsync<TR>(cacheName, del, [t1, t2], expiry);
  86. }
  87. public async Task<TR> AdGetAsync<TR, T1, T2, T3>(String cacheName, Func<T1, T2, T3, Task<TR>> del, T1 t1, T2 t2, T3 t3, TimeSpan? expiry = default) where TR : class
  88. {
  89. return await AdGetAsync<TR>(cacheName, del, [t1, t2, t3], expiry);
  90. }
  91. private async Task<T> AdGetAsync<T>(string cacheName, Delegate del, Object[] obs, TimeSpan? expiry) where T : class
  92. {
  93. var key = Key(cacheName, obs);
  94. // 使用分布式锁
  95. using (_cacheProvider.Cache.AcquireLock($@"lock:AdGetAsync:{cacheName}", 1000))
  96. {
  97. var value = Get<T>(key);
  98. if (value == null)
  99. {
  100. value = await ((dynamic)del).DynamicInvoke(obs);
  101. if (expiry == null)
  102. {
  103. Set(key, value);
  104. }
  105. else
  106. {
  107. Set(key, value, (TimeSpan)expiry);
  108. }
  109. }
  110. return value;
  111. }
  112. }
  113. public T Get<T>(String cacheName, object t1)
  114. {
  115. return Get<T>(cacheName, [t1]);
  116. }
  117. public T Get<T>(String cacheName, object t1, object t2)
  118. {
  119. return Get<T>(cacheName, [t1, t2]);
  120. }
  121. public T Get<T>(String cacheName, object t1, object t2, object t3)
  122. {
  123. return Get<T>(cacheName, [t1, t2, t3]);
  124. }
  125. private T Get<T>(String cacheName, Object[] obs)
  126. {
  127. var key = Key(cacheName, obs);
  128. return Get<T>(key);
  129. }
  130. private static string Key(string cacheName, object[] obs)
  131. {
  132. if (obs.OfType<TimeSpan>().Any()) throw new Exception("缓存参数类型不能是:TimeSpan类型");
  133. StringBuilder sb = new(cacheName);
  134. if (obs is { Length: > 0 })
  135. {
  136. sb.Append(':');
  137. foreach (var a in obs) sb.Append($"<{KeySingle(a)}>");
  138. }
  139. return sb.ToString();
  140. }
  141. private static string KeySingle(object t)
  142. {
  143. return t.GetType().IsClass && !t.GetType().IsPrimitive ? JsonConvert.SerializeObject(t) : t.ToString();
  144. }
  145. /// <summary>
  146. /// 获取缓存的剩余生存时间
  147. /// </summary>
  148. /// <param name="key"></param>
  149. /// <returns></returns>
  150. [NonAction]
  151. public TimeSpan GetExpire(string key)
  152. {
  153. return _cacheProvider.Cache.GetExpire(key);
  154. }
  155. /// <summary>
  156. /// 获取缓存
  157. /// </summary>
  158. /// <typeparam name="T"></typeparam>
  159. /// <param name="key"></param>
  160. /// <returns></returns>
  161. [NonAction]
  162. public T Get<T>(string key)
  163. {
  164. return _cacheProvider.Cache.Get<T>($"{_cacheOptions.Prefix}{key}");
  165. }
  166. /// <summary>
  167. /// 删除缓存 🔖
  168. /// </summary>
  169. /// <param name="key"></param>
  170. /// <returns></returns>
  171. [ApiDescriptionSettings(Name = "Delete"), HttpPost]
  172. [DisplayName("删除缓存")]
  173. public int Remove(string key)
  174. {
  175. return _cacheProvider.Cache.Remove($"{_cacheOptions.Prefix}{key}");
  176. }
  177. /// <summary>
  178. /// 清空所有缓存 🔖
  179. /// </summary>
  180. /// <returns></returns>
  181. [DisplayName("清空所有缓存")]
  182. [ApiDescriptionSettings(Name = "Clear"), HttpPost]
  183. public void Clear()
  184. {
  185. _cacheProvider.Cache.Clear();
  186. Cache.Default.Clear();
  187. }
  188. /// <summary>
  189. /// 检查缓存是否存在
  190. /// </summary>
  191. /// <param name="key">键</param>
  192. /// <returns></returns>
  193. [NonAction]
  194. public bool ExistKey(string key)
  195. {
  196. return _cacheProvider.Cache.ContainsKey($"{_cacheOptions.Prefix}{key}");
  197. }
  198. /// <summary>
  199. /// 根据键名前缀删除缓存 🔖
  200. /// </summary>
  201. /// <param name="prefixKey">键名前缀</param>
  202. /// <returns></returns>
  203. [ApiDescriptionSettings(Name = "DeleteByPreKey"), HttpPost]
  204. [DisplayName("根据键名前缀删除缓存")]
  205. public int RemoveByPrefixKey(string prefixKey)
  206. {
  207. var delKeys = _cacheProvider.Cache == Cache.Default
  208. ? _cacheProvider.Cache.Keys.Where(u => u.StartsWith($"{_cacheOptions.Prefix}{prefixKey}")).ToArray()
  209. : ((FullRedis)_cacheProvider.Cache).Search($"{_cacheOptions.Prefix}{prefixKey}*", int.MaxValue).ToArray();
  210. return _cacheProvider.Cache.Remove(delKeys);
  211. }
  212. /// <summary>
  213. /// 根据键名前缀获取键名集合 🔖
  214. /// </summary>
  215. /// <param name="prefixKey">键名前缀</param>
  216. /// <returns></returns>
  217. [DisplayName("根据键名前缀获取键名集合")]
  218. public List<string> GetKeysByPrefixKey(string prefixKey)
  219. {
  220. return _cacheProvider.Cache == Cache.Default
  221. ? _cacheProvider.Cache.Keys.Where(u => u.StartsWith($"{_cacheOptions.Prefix}{prefixKey}")).Select(u => u[_cacheOptions.Prefix.Length..]).ToList()
  222. : ((FullRedis)_cacheProvider.Cache).Search($"{_cacheOptions.Prefix}{prefixKey}*", int.MaxValue).Select(u => u[_cacheOptions.Prefix.Length..]).ToList();
  223. }
  224. /// <summary>
  225. /// 获取缓存值 🔖
  226. /// </summary>
  227. /// <param name="key"></param>
  228. /// <returns></returns>
  229. [DisplayName("获取缓存值")]
  230. public object GetValue(string key)
  231. {
  232. if (string.IsNullOrEmpty(key)) return null;
  233. if (Regex.IsMatch(key, @"%[0-9a-fA-F]{2}"))
  234. key = HttpUtility.UrlDecode(key);
  235. var fullKey = $"{_cacheOptions.Prefix}{key}";
  236. if (_cacheProvider.Cache == Cache.Default)
  237. return _cacheProvider.Cache.Get<object>(fullKey);
  238. if (_cacheProvider.Cache is FullRedis redisCache)
  239. {
  240. if (!redisCache.ContainsKey(fullKey))
  241. return null;
  242. try
  243. {
  244. var keyType = redisCache.TYPE(fullKey)?.ToLower();
  245. switch (keyType)
  246. {
  247. case "string":
  248. return redisCache.Get<string>(fullKey);
  249. case "list":
  250. var list = redisCache.GetList<string>(fullKey);
  251. return list?.ToList();
  252. case "hash":
  253. var hash = redisCache.GetDictionary<string>(fullKey);
  254. return hash?.ToDictionary(k => k.Key, v => v.Value);
  255. case "set":
  256. var set = redisCache.GetSet<string>(fullKey);
  257. return set?.ToArray();
  258. case "zset":
  259. var sortedSet = redisCache.GetSortedSet<string>(fullKey);
  260. return sortedSet?.Range(0, -1)?.ToList();
  261. case "none":
  262. return null;
  263. default:
  264. // 未知类型或特殊类型
  265. return new Dictionary<string, object>
  266. {
  267. { "key", key },
  268. { "type", keyType ?? "unknown" },
  269. { "message", "无法使用标准方式获取此类型数据" }
  270. };
  271. }
  272. }
  273. catch (Exception ex)
  274. {
  275. return new Dictionary<string, object>
  276. {
  277. { "key", key },
  278. { "error", ex.Message },
  279. { "type", "exception" }
  280. };
  281. }
  282. }
  283. return _cacheProvider.Cache.Get<object>(fullKey);
  284. }
  285. /// <summary>
  286. /// 获取或添加缓存(在数据不存在时执行委托请求数据)
  287. /// </summary>
  288. /// <typeparam name="T"></typeparam>
  289. /// <param name="key"></param>
  290. /// <param name="callback"></param>
  291. /// <param name="expire">过期时间,单位秒</param>
  292. /// <returns></returns>
  293. [NonAction]
  294. public T GetOrAdd<T>(string key, Func<string, T> callback, int expire = -1)
  295. {
  296. if (string.IsNullOrWhiteSpace(key)) return default;
  297. return _cacheProvider.Cache.GetOrAdd($"{_cacheOptions.Prefix}{key}", callback, expire);
  298. }
  299. /// <summary>
  300. /// Hash匹配
  301. /// </summary>
  302. /// <typeparam name="T"></typeparam>
  303. /// <param name="key"></param>
  304. /// <returns></returns>
  305. [NonAction]
  306. public IDictionary<String, T> GetHashMap<T>(string key)
  307. {
  308. return _cacheProvider.Cache.GetDictionary<T>(key);
  309. }
  310. /// <summary>
  311. /// 批量添加HASH
  312. /// </summary>
  313. /// <typeparam name="T"></typeparam>
  314. /// <param name="key"></param>
  315. /// <param name="dic"></param>
  316. /// <returns></returns>
  317. [NonAction]
  318. public bool HashSet<T>(string key, Dictionary<string, T> dic)
  319. {
  320. var hash = GetHashMap<T>(key);
  321. foreach (var v in dic)
  322. {
  323. hash.Add(v);
  324. }
  325. return true;
  326. }
  327. /// <summary>
  328. /// 添加一条HASH
  329. /// </summary>
  330. /// <typeparam name="T"></typeparam>
  331. /// <param name="key"></param>
  332. /// <param name="hashKey"></param>
  333. /// <param name="value"></param>
  334. [NonAction]
  335. public void HashAdd<T>(string key, string hashKey, T value)
  336. {
  337. var hash = GetHashMap<T>(key);
  338. hash.Add(hashKey, value);
  339. }
  340. /// <summary>
  341. /// 添加或更新一条HASH
  342. /// </summary>
  343. /// <typeparam name="T"></typeparam>
  344. /// <param name="key"></param>
  345. /// <param name="hashKey"></param>
  346. /// <param name="value"></param>
  347. [NonAction]
  348. public void HashAddOrUpdate<T>(string key, string hashKey, T value)
  349. {
  350. var hash = GetHashMap<T>(key);
  351. if (hash.ContainsKey(hashKey))
  352. hash[hashKey] = value;
  353. else
  354. hash.Add(hashKey, value);
  355. }
  356. /// <summary>
  357. /// 获取多条HASH
  358. /// </summary>
  359. /// <typeparam name="T"></typeparam>
  360. /// <param name="key"></param>
  361. /// <param name="fields"></param>
  362. /// <returns></returns>
  363. [NonAction]
  364. public List<T> HashGet<T>(string key, params string[] fields)
  365. {
  366. var hash = GetHashMap<T>(key);
  367. return hash.Where(t => fields.Any(c => t.Key == c)).Select(t => t.Value).ToList();
  368. }
  369. /// <summary>
  370. /// 获取一条HASH
  371. /// </summary>
  372. /// <typeparam name="T"></typeparam>
  373. /// <param name="key"></param>
  374. /// <param name="field"></param>
  375. /// <returns></returns>
  376. [NonAction]
  377. public T HashGetOne<T>(string key, string field)
  378. {
  379. var hash = GetHashMap<T>(key);
  380. return hash.TryGetValue(field, out T value) ? value : default;
  381. }
  382. /// <summary>
  383. /// 根据KEY获取所有HASH
  384. /// </summary>
  385. /// <typeparam name="T"></typeparam>
  386. /// <param name="key"></param>
  387. /// <returns></returns>
  388. [NonAction]
  389. public IDictionary<string, T> HashGetAll<T>(string key)
  390. {
  391. var hash = GetHashMap<T>(key);
  392. return hash;
  393. }
  394. /// <summary>
  395. /// 删除HASH
  396. /// </summary>
  397. /// <typeparam name="T"></typeparam>
  398. /// <param name="key"></param>
  399. /// <param name="fields"></param>
  400. /// <returns></returns>
  401. [NonAction]
  402. public int HashDel<T>(string key, params string[] fields)
  403. {
  404. var hash = GetHashMap<T>(key);
  405. fields.ToList().ForEach(t => hash.Remove(t));
  406. return fields.Length;
  407. }
  408. ///// <summary>
  409. ///// 搜索HASH
  410. ///// </summary>
  411. ///// <typeparam name="T"></typeparam>
  412. ///// <param name="key"></param>
  413. ///// <param name="searchModel"></param>
  414. ///// <returns></returns>
  415. //[NonAction]
  416. //public List<KeyValuePair<string, T>> HashSearch<T>(string key, SearchModel searchModel)
  417. //{
  418. // var hash = GetHashMap<T>(key);
  419. // return hash.Search(searchModel).ToList();
  420. //}
  421. ///// <summary>
  422. ///// 搜索HASH
  423. ///// </summary>
  424. ///// <typeparam name="T"></typeparam>
  425. ///// <param name="key"></param>
  426. ///// <param name="pattern"></param>
  427. ///// <param name="count"></param>
  428. ///// <returns></returns>
  429. //[NonAction]
  430. //public List<KeyValuePair<string, T>> HashSearch<T>(string key, string pattern, int count)
  431. //{
  432. // var hash = GetHashMap<T>(key);
  433. // return hash.Search(pattern, count).ToList();
  434. //}
  435. }