SysCacheService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. using NewLife.Reflection;
  7. using Newtonsoft.Json;
  8. namespace Admin.NET.Core.Service;
  9. /// <summary>
  10. /// 系统缓存服务 🧩
  11. /// </summary>
  12. [ApiDescriptionSettings(Order = 400)]
  13. public class SysCacheService : IDynamicApiController, ISingleton
  14. {
  15. private static ICacheProvider _cacheProvider;
  16. private readonly CacheOptions _cacheOptions;
  17. public SysCacheService(ICacheProvider cacheProvider, IOptions<CacheOptions> cacheOptions)
  18. {
  19. _cacheProvider = cacheProvider;
  20. _cacheOptions = cacheOptions.Value;
  21. }
  22. /// <summary>
  23. /// 申请分布式锁
  24. /// </summary>
  25. /// <param name="key">要锁定的key</param>
  26. /// <param name="msTimeout">申请锁等待的时间,单位毫秒</param>
  27. /// <param name="msExpire">锁过期时间,超过该时间没有主动是放则自动是放,必须整数秒,单位毫秒</param>
  28. /// <param name="throwOnFailure">失败时是否抛出异常,如不抛出异常,可通过判断返回null得知申请锁失败</param>
  29. /// <returns></returns>
  30. [DisplayName("申请分布式锁")]
  31. public IDisposable? BeginCacheLock(string key, int msTimeout = 500, int msExpire = 10000, bool throwOnFailure = true)
  32. {
  33. try
  34. {
  35. return _cacheProvider.Cache.AcquireLock(key, msTimeout, msExpire, throwOnFailure);
  36. }
  37. catch
  38. {
  39. return null;
  40. }
  41. }
  42. /// <summary>
  43. /// 获取缓存键名集合 🔖
  44. /// </summary>
  45. /// <returns></returns>
  46. [DisplayName("获取缓存键名集合")]
  47. public List<string> GetKeyList()
  48. {
  49. return _cacheProvider.Cache == Cache.Default
  50. ? _cacheProvider.Cache.Keys.Where(u => u.StartsWith(_cacheOptions.Prefix)).Select(u => u[_cacheOptions.Prefix.Length..]).OrderBy(u => u).ToList()
  51. : ((FullRedis)_cacheProvider.Cache).Search($"{_cacheOptions.Prefix}*", int.MaxValue).Select(u => u[_cacheOptions.Prefix.Length..]).OrderBy(u => u).ToList();
  52. }
  53. /// <summary>
  54. /// 增加缓存
  55. /// </summary>
  56. /// <param name="key"></param>
  57. /// <param name="value"></param>
  58. /// <returns></returns>
  59. [NonAction]
  60. public bool Set(string key, object value)
  61. {
  62. if (string.IsNullOrWhiteSpace(key)) return false;
  63. return _cacheProvider.Cache.Set($"{_cacheOptions.Prefix}{key}", value);
  64. }
  65. /// <summary>
  66. /// 增加缓存并设置过期时间
  67. /// </summary>
  68. /// <param name="key"></param>
  69. /// <param name="value"></param>
  70. /// <param name="expire"></param>
  71. /// <returns></returns>
  72. [NonAction]
  73. public bool Set(string key, object value, TimeSpan expire)
  74. {
  75. if (string.IsNullOrWhiteSpace(key)) return false;
  76. return _cacheProvider.Cache.Set($"{_cacheOptions.Prefix}{key}", value, expire);
  77. }
  78. public async Task<TR> AdGetAsync<TR>(String cacheName, Func<Task<TR>> del, TimeSpan? expiry = default(TimeSpan?)) where TR : class
  79. {
  80. return await AdGetAsync<TR>(cacheName, del, new object[] { }, expiry);
  81. }
  82. public async Task<TR> AdGetAsync<TR, T1>(String cacheName, Func<T1, Task<TR>> del, T1 t1, TimeSpan? expiry = default(TimeSpan?)) where TR : class
  83. {
  84. return await AdGetAsync<TR>(cacheName, del, new object[] { t1 }, expiry);
  85. }
  86. public async Task<TR> AdGetAsync<TR, T1, T2>(String cacheName, Func<T1, T2, Task<TR>> del, T1 t1, T2 t2, TimeSpan? expiry = default(TimeSpan?)) where TR : class
  87. {
  88. return await AdGetAsync<TR>(cacheName, del, new object[] { t1, t2 }, expiry);
  89. }
  90. 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(TimeSpan?)) where TR : class
  91. {
  92. return await AdGetAsync<TR>(cacheName, del, new object[] { t1, t2, t3 }, expiry);
  93. }
  94. private async Task<T> AdGetAsync<T>(string cacheName, Delegate del, Object[] obs, TimeSpan? expiry) where T : class
  95. {
  96. var key = Key(cacheName, obs);
  97. // 使用分布式锁
  98. using (_cacheProvider.Cache.AcquireLock($@"lock:AdGetAsync:{cacheName}", 1000))
  99. {
  100. var value = Get<T>(key);
  101. value ??= await ((dynamic)del).DynamicInvokeAsync(obs);
  102. Set(key, value);
  103. return value;
  104. }
  105. }
  106. public T Get<T>(String cacheName, object t1)
  107. {
  108. return Get<T>(cacheName, new object[] { t1 });
  109. }
  110. public T Get<T>(String cacheName, object t1, object t2)
  111. {
  112. return Get<T>(cacheName, new object[] { t1, t2 });
  113. }
  114. public T Get<T>(String cacheName, object t1, object t2, object t3)
  115. {
  116. return Get<T>(cacheName, new object[] { t1, t2, t3 });
  117. }
  118. private T Get<T>(String cacheName, Object[] obs)
  119. {
  120. var key = cacheName + ":" + obs.Aggregate(string.Empty, (current, o) => current + $"<{o}>");
  121. return Get<T>(key);
  122. }
  123. private static string Key(string cacheName, object[] obs)
  124. {
  125. foreach (var obj in obs)
  126. {
  127. if (obj is TimeSpan)
  128. {
  129. throw new Exception("缓存参数类型不能能是:TimeSpan类型");
  130. }
  131. }
  132. StringBuilder sb = new StringBuilder(cacheName + ":");
  133. foreach (var a in obs)
  134. {
  135. sb.Append($@"<{KeySingle(a)}>");
  136. }
  137. return sb.ToString();
  138. }
  139. public static string KeySingle(object t)
  140. {
  141. if (t.GetType().IsClass && !t.GetType().IsPrimitive)
  142. {
  143. return JsonConvert.SerializeObject(t);
  144. }
  145. return t?.ToString();
  146. }
  147. /// <summary>
  148. /// 获取缓存的剩余生存时间
  149. /// </summary>
  150. /// <param name="key"></param>
  151. /// <returns></returns>
  152. [NonAction]
  153. public TimeSpan GetExpire(string key)
  154. {
  155. return _cacheProvider.Cache.GetExpire(key);
  156. }
  157. /// <summary>
  158. /// 获取缓存
  159. /// </summary>
  160. /// <typeparam name="T"></typeparam>
  161. /// <param name="key"></param>
  162. /// <returns></returns>
  163. [NonAction]
  164. public T Get<T>(string key)
  165. {
  166. return _cacheProvider.Cache.Get<T>($"{_cacheOptions.Prefix}{key}");
  167. }
  168. /// <summary>
  169. /// 删除缓存 🔖
  170. /// </summary>
  171. /// <param name="key"></param>
  172. /// <returns></returns>
  173. [ApiDescriptionSettings(Name = "Delete"), HttpPost]
  174. [DisplayName("删除缓存")]
  175. public int Remove(string key)
  176. {
  177. return _cacheProvider.Cache.Remove($"{_cacheOptions.Prefix}{key}");
  178. }
  179. /// <summary>
  180. /// 清空所有缓存 🔖
  181. /// </summary>
  182. /// <returns></returns>
  183. [DisplayName("清空所有缓存")]
  184. [ApiDescriptionSettings(Name = "Clear"), HttpPost]
  185. public void Clear()
  186. {
  187. _cacheProvider.Cache.Clear();
  188. Cache.Default.Clear();
  189. }
  190. /// <summary>
  191. /// 检查缓存是否存在
  192. /// </summary>
  193. /// <param name="key">键</param>
  194. /// <returns></returns>
  195. [NonAction]
  196. public bool ExistKey(string key)
  197. {
  198. return _cacheProvider.Cache.ContainsKey($"{_cacheOptions.Prefix}{key}");
  199. }
  200. /// <summary>
  201. /// 根据键名前缀删除缓存 🔖
  202. /// </summary>
  203. /// <param name="prefixKey">键名前缀</param>
  204. /// <returns></returns>
  205. [ApiDescriptionSettings(Name = "DeleteByPreKey"), HttpPost]
  206. [DisplayName("根据键名前缀删除缓存")]
  207. public int RemoveByPrefixKey(string prefixKey)
  208. {
  209. var delKeys = _cacheProvider.Cache == Cache.Default
  210. ? _cacheProvider.Cache.Keys.Where(u => u.StartsWith($"{_cacheOptions.Prefix}{prefixKey}")).ToArray()
  211. : ((FullRedis)_cacheProvider.Cache).Search($"{_cacheOptions.Prefix}{prefixKey}*", int.MaxValue).ToArray();
  212. return _cacheProvider.Cache.Remove(delKeys);
  213. }
  214. /// <summary>
  215. /// 根据键名前缀获取键名集合 🔖
  216. /// </summary>
  217. /// <param name="prefixKey">键名前缀</param>
  218. /// <returns></returns>
  219. [DisplayName("根据键名前缀获取键名集合")]
  220. public List<string> GetKeysByPrefixKey(string prefixKey)
  221. {
  222. return _cacheProvider.Cache == Cache.Default
  223. ? _cacheProvider.Cache.Keys.Where(u => u.StartsWith($"{_cacheOptions.Prefix}{prefixKey}")).Select(u => u[_cacheOptions.Prefix.Length..]).ToList()
  224. : ((FullRedis)_cacheProvider.Cache).Search($"{_cacheOptions.Prefix}{prefixKey}*", int.MaxValue).Select(u => u[_cacheOptions.Prefix.Length..]).ToList();
  225. }
  226. /// <summary>
  227. /// 获取缓存值 🔖
  228. /// </summary>
  229. /// <param name="key"></param>
  230. /// <returns></returns>
  231. [DisplayName("获取缓存值")]
  232. public object GetValue(string key)
  233. {
  234. // 若Key经过URL编码则进行解码
  235. if (Regex.IsMatch(key, @"%[0-9a-fA-F]{2}"))
  236. key = HttpUtility.UrlDecode(key);
  237. return _cacheProvider.Cache == Cache.Default
  238. ? _cacheProvider.Cache.Get<object>($"{_cacheOptions.Prefix}{key}")
  239. : _cacheProvider.Cache.Get<string>($"{_cacheOptions.Prefix}{key}");
  240. }
  241. /// <summary>
  242. /// 获取或添加缓存(在数据不存在时执行委托请求数据)
  243. /// </summary>
  244. /// <typeparam name="T"></typeparam>
  245. /// <param name="key"></param>
  246. /// <param name="callback"></param>
  247. /// <param name="expire">过期时间,单位秒</param>
  248. /// <returns></returns>
  249. [NonAction]
  250. public T GetOrAdd<T>(string key, Func<string, T> callback, int expire = -1)
  251. {
  252. if (string.IsNullOrWhiteSpace(key)) return default;
  253. return _cacheProvider.Cache.GetOrAdd($"{_cacheOptions.Prefix}{key}", callback, expire);
  254. }
  255. /// <summary>
  256. /// Hash匹配
  257. /// </summary>
  258. /// <typeparam name="T"></typeparam>
  259. /// <param name="key"></param>
  260. /// <returns></returns>
  261. [NonAction]
  262. public IDictionary<String, T> GetHashMap<T>(string key)
  263. {
  264. return _cacheProvider.Cache.GetDictionary<T>(key);
  265. }
  266. /// <summary>
  267. /// 批量添加HASH
  268. /// </summary>
  269. /// <typeparam name="T"></typeparam>
  270. /// <param name="key"></param>
  271. /// <param name="dic"></param>
  272. /// <returns></returns>
  273. [NonAction]
  274. public bool HashSet<T>(string key, Dictionary<string, T> dic)
  275. {
  276. var hash = GetHashMap<T>(key);
  277. foreach (var v in dic)
  278. {
  279. hash.Add(v);
  280. }
  281. return true;
  282. }
  283. /// <summary>
  284. /// 添加一条HASH
  285. /// </summary>
  286. /// <typeparam name="T"></typeparam>
  287. /// <param name="key"></param>
  288. /// <param name="hashKey"></param>
  289. /// <param name="value"></param>
  290. [NonAction]
  291. public void HashAdd<T>(string key, string hashKey, T value)
  292. {
  293. var hash = GetHashMap<T>(key);
  294. hash.Add(hashKey, value);
  295. }
  296. /// <summary>
  297. /// 获取多条HASH
  298. /// </summary>
  299. /// <typeparam name="T"></typeparam>
  300. /// <param name="key"></param>
  301. /// <param name="fields"></param>
  302. /// <returns></returns>
  303. [NonAction]
  304. public List<T> HashGet<T>(string key, params string[] fields)
  305. {
  306. var hash = GetHashMap<T>(key);
  307. return hash.Where(t => fields.Any(c => t.Key == c)).Select(t => t.Value).ToList();
  308. }
  309. /// <summary>
  310. /// 获取一条HASH
  311. /// </summary>
  312. /// <typeparam name="T"></typeparam>
  313. /// <param name="key"></param>
  314. /// <param name="field"></param>
  315. /// <returns></returns>
  316. [NonAction]
  317. public T HashGetOne<T>(string key, string field)
  318. {
  319. var hash = GetHashMap<T>(key);
  320. var value = hash.GetValue(field);
  321. if (value == null)
  322. {
  323. return default(T);
  324. }
  325. return (T)hash.GetValue(field);
  326. }
  327. /// <summary>
  328. /// 根据KEY获取所有HASH
  329. /// </summary>
  330. /// <typeparam name="T"></typeparam>
  331. /// <param name="key"></param>
  332. /// <returns></returns>
  333. [NonAction]
  334. public IDictionary<string, T> HashGetAll<T>(string key)
  335. {
  336. var hash = GetHashMap<T>(key);
  337. return hash;
  338. }
  339. /// <summary>
  340. /// 删除HASH
  341. /// </summary>
  342. /// <typeparam name="T"></typeparam>
  343. /// <param name="key"></param>
  344. /// <param name="fields"></param>
  345. /// <returns></returns>
  346. [NonAction]
  347. public int HashDel<T>(string key, params string[] fields)
  348. {
  349. var hash = GetHashMap<T>(key);
  350. fields.ToList().ForEach(t => hash.Remove(t));
  351. return fields.Length;
  352. }
  353. ///// <summary>
  354. ///// 搜索HASH
  355. ///// </summary>
  356. ///// <typeparam name="T"></typeparam>
  357. ///// <param name="key"></param>
  358. ///// <param name="searchModel"></param>
  359. ///// <returns></returns>
  360. //[NonAction]
  361. //public List<KeyValuePair<string, T>> HashSearch<T>(string key, SearchModel searchModel)
  362. //{
  363. // var hash = GetHashMap<T>(key);
  364. // return hash.Search(searchModel).ToList();
  365. //}
  366. ///// <summary>
  367. ///// 搜索HASH
  368. ///// </summary>
  369. ///// <typeparam name="T"></typeparam>
  370. ///// <param name="key"></param>
  371. ///// <param name="pattern"></param>
  372. ///// <param name="count"></param>
  373. ///// <returns></returns>
  374. //[NonAction]
  375. //public List<KeyValuePair<string, T>> HashSearch<T>(string key, string pattern, int count)
  376. //{
  377. // var hash = GetHashMap<T>(key);
  378. // return hash.Search(pattern, count).ToList();
  379. //}
  380. }