CacheService.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using Furion.JsonSerialization;
  2. using Microsoft.Extensions.Caching.Distributed;
  3. using System.Text;
  4. using YBEE.EQM.Core;
  5. namespace YBEE.EQM.Application;
  6. /// <summary>
  7. /// 缓存服务
  8. /// </summary>
  9. public class CacheService : ICacheService, ISingleton
  10. {
  11. private readonly IDistributedCache _cache;
  12. public CacheService(IDistributedCache cache)
  13. {
  14. _cache = cache;
  15. }
  16. /// <summary>
  17. /// 获取数据范围缓存(机构Id集合)
  18. /// </summary>
  19. /// <param name="userId"></param>
  20. /// <returns></returns>
  21. public async Task<List<int>> GetDataScope(int userId)
  22. {
  23. var cacheKey = $"{CommonConst.CACHE_KEY_DATASCOPE}{userId}";
  24. var res = await _cache.GetStringAsync(cacheKey);
  25. return string.IsNullOrWhiteSpace(res) ? null : JSON.Deserialize<List<int>>(res);
  26. }
  27. /// <summary>
  28. /// 缓存数据范围(机构Id集合)
  29. /// </summary>
  30. /// <param name="userId"></param>
  31. /// <param name="dataScopes"></param>
  32. /// <returns></returns>
  33. public async Task SetDataScope(int userId, List<int> dataScopes)
  34. {
  35. var cacheKey = $"{CommonConst.CACHE_KEY_DATASCOPE}{userId}";
  36. await _cache.SetStringAsync(cacheKey, JSON.Serialize(dataScopes));
  37. await AddCacheKey(cacheKey);
  38. }
  39. #region Permission
  40. /// <summary>
  41. /// 获取权限缓存(按钮)
  42. /// </summary>
  43. /// <param name="userId"></param>
  44. /// <returns></returns>
  45. public async Task<List<string>> GetPermission(int userId)
  46. {
  47. var cacheKey = $"{CommonConst.CACHE_KEY_PERMISSION}{userId}";
  48. var res = await _cache.GetStringAsync(cacheKey);
  49. return string.IsNullOrWhiteSpace(res) ? null : JSON.Deserialize<List<string>>(res);
  50. }
  51. /// <summary>
  52. /// 删除权限缓存
  53. /// </summary>
  54. /// <param name="userId"></param>
  55. /// <returns></returns>
  56. public async Task RemovePermission(int userId)
  57. {
  58. var cacheKey = $"{CommonConst.CACHE_KEY_PERMISSION}{userId}";
  59. await RemoveAsync(cacheKey);
  60. }
  61. /// <summary>
  62. /// 缓存权限
  63. /// </summary>
  64. /// <param name="userId"></param>
  65. /// <param name="permissions"></param>
  66. /// <returns></returns>
  67. public async Task SetPermission(int userId, List<string> permissions)
  68. {
  69. var cacheKey = $"{CommonConst.CACHE_KEY_PERMISSION}{userId}";
  70. await _cache.SetStringAsync(cacheKey, JSON.Serialize(permissions));
  71. await AddCacheKey(cacheKey);
  72. }
  73. #endregion
  74. #region 角色数据范围权限缓存
  75. /// <summary>
  76. /// 获取角色数据范围权限缓存
  77. /// </summary>
  78. /// <param name="userId"></param>
  79. /// <returns></returns>
  80. public async Task<List<SysRoleOutput>> GetRoleDataScope(int userId)
  81. {
  82. var cacheKey = $"{CommonConst.CACHE_KEY_ROLE_DATASCOPE}{userId}";
  83. var res = await _cache.GetStringAsync(cacheKey);
  84. return string.IsNullOrWhiteSpace(res) ? null : JSON.Deserialize<List<SysRoleOutput>>(res);
  85. }
  86. /// <summary>
  87. /// 删除角色数据范围权限缓存
  88. /// </summary>
  89. /// <param name="userId"></param>
  90. /// <returns></returns>
  91. public async Task RemoveRoleDataScope(int userId)
  92. {
  93. var cacheKey = $"{CommonConst.CACHE_KEY_ROLE_DATASCOPE}{userId}";
  94. await RemoveAsync(cacheKey);
  95. }
  96. /// <summary>
  97. /// 缓存角色数据范围权限
  98. /// </summary>
  99. /// <param name="userId"></param>
  100. /// <param name="roles"></param>
  101. /// <returns></returns>
  102. public async Task SetRoleDataScope(int userId, List<SysRoleOutput> roles)
  103. {
  104. var cacheKey = $"{CommonConst.CACHE_KEY_ROLE_DATASCOPE}{userId}";
  105. await _cache.SetStringAsync(cacheKey, JSON.Serialize(roles));
  106. await AddCacheKey(cacheKey);
  107. }
  108. #endregion
  109. /// <summary>
  110. /// 获取所有缓存关键字
  111. /// </summary>
  112. /// <returns></returns>
  113. //[HttpGet("sysCache/keyList")]
  114. public async Task<List<string>> GetAllCacheKeys()
  115. {
  116. var res = await _cache.GetStringAsync(CommonConst.CACHE_KEY_ALL);
  117. return string.IsNullOrWhiteSpace(res) ? null : JSON.Deserialize<List<string>>(res);
  118. }
  119. /// <summary>
  120. /// 删除指定关键字缓存
  121. /// </summary>
  122. /// <param name="key"></param>
  123. /// <returns></returns>
  124. //[HttpGet("sysCache/remove")]
  125. public async Task RemoveAsync(string key)
  126. {
  127. await _cache.RemoveAsync(key);
  128. await DelCacheKey(key);
  129. }
  130. /// <summary>
  131. /// 删除某特征关键字缓存
  132. /// </summary>
  133. /// <param name="key"></param>
  134. /// <returns></returns>
  135. public async Task DelByPatternAsync(string key)
  136. {
  137. var allkeys = await GetAllCacheKeys();
  138. var delAllkeys = allkeys?.Where(u => u.Contains(key)).ToList();
  139. // 删除相应的缓存
  140. delAllkeys?.ForEach(u =>
  141. {
  142. _cache.Remove(u);
  143. });
  144. // 更新所有缓存键
  145. allkeys = allkeys?.Where(u => !u.Contains(key)).ToList();
  146. await _cache.SetStringAsync(CommonConst.CACHE_KEY_ALL, JSON.Serialize(allkeys));
  147. }
  148. /// <summary>
  149. /// 设置缓存
  150. /// </summary>
  151. /// <param name="cacheKey"></param>
  152. /// <param name="value"></param>
  153. /// <returns></returns>
  154. public async Task SetAsync(string cacheKey, object value)
  155. {
  156. await _cache.SetAsync(cacheKey, Encoding.UTF8.GetBytes(JSON.Serialize(value)));
  157. await AddCacheKey(cacheKey);
  158. }
  159. /// <summary>
  160. /// 设置缓存
  161. /// </summary>
  162. /// <param name="cacheKey"></param>
  163. /// <param name="value"></param>
  164. /// <returns></returns>
  165. public async Task SetStringAsync(string cacheKey, string value)
  166. {
  167. await _cache.SetStringAsync(cacheKey, value);
  168. await AddCacheKey(cacheKey);
  169. }
  170. /// <summary>
  171. /// 获取缓存
  172. /// </summary>
  173. /// <param name="cacheKey"></param>
  174. /// <returns></returns>
  175. //[HttpGet("sysCache/detail")]
  176. public async Task<string> GetStringAsync(string cacheKey)
  177. {
  178. return await _cache.GetStringAsync(cacheKey);
  179. }
  180. /// <summary>
  181. /// 获取缓存
  182. /// </summary>
  183. /// <typeparam name="T"></typeparam>
  184. /// <param name="cacheKey"></param>
  185. /// <returns></returns>
  186. public async Task<T> GetAsync<T>(string cacheKey)
  187. {
  188. var res = await _cache.GetAsync(cacheKey);
  189. return res == null ? default : JSON.Deserialize<T>(Encoding.UTF8.GetString(res));
  190. }
  191. /// <summary>
  192. /// 检查给定 key 是否存在
  193. /// </summary>
  194. /// <param name="cacheKey">键</param>
  195. /// <returns></returns>
  196. public bool Exists(string cacheKey)
  197. {
  198. var res = _cache.GetStringAsync(CommonConst.CACHE_KEY_ALL).GetAwaiter().GetResult();
  199. var allkeys = string.IsNullOrWhiteSpace(res) ? new HashSet<string>() : JSON.Deserialize<HashSet<string>>(res);
  200. return allkeys.Any(_ => _ == cacheKey);
  201. }
  202. /// <summary>
  203. /// 增加缓存Key
  204. /// </summary>
  205. /// <param name="cacheKey"></param>
  206. /// <returns></returns>
  207. public async Task AddCacheKey(string cacheKey)
  208. {
  209. var res = await _cache.GetStringAsync(CommonConst.CACHE_KEY_ALL);
  210. var allkeys = string.IsNullOrWhiteSpace(res) ? new HashSet<string>() : JSON.Deserialize<HashSet<string>>(res);
  211. if (!allkeys.Any(m => m == cacheKey))
  212. {
  213. allkeys.Add(cacheKey);
  214. await _cache.SetStringAsync(CommonConst.CACHE_KEY_ALL, JSON.Serialize(allkeys));
  215. }
  216. }
  217. /// <summary>
  218. /// 按KEY删除缓存
  219. /// </summary>
  220. /// <param name="cacheKey"></param>
  221. /// <returns></returns>
  222. public async Task DelCacheKey(string cacheKey)
  223. {
  224. var res = await _cache.GetStringAsync(CommonConst.CACHE_KEY_ALL);
  225. var allkeys = string.IsNullOrWhiteSpace(res) ? new HashSet<string>() : JSON.Deserialize<HashSet<string>>(res);
  226. if (allkeys.Any(m => m == cacheKey))
  227. {
  228. allkeys.Remove(cacheKey);
  229. await _cache.SetStringAsync(CommonConst.CACHE_KEY_ALL, JSON.Serialize(allkeys));
  230. }
  231. }
  232. /// <summary>
  233. /// 获取菜单缓存
  234. /// </summary>
  235. /// <param name="userId"></param>
  236. /// <returns></returns>
  237. public async Task<List<AntMenuOutput>> GetMenu(int userId)
  238. {
  239. var cacheKey = $"{CommonConst.CACHE_KEY_MENU}{userId}";
  240. var res = await _cache.GetStringAsync(cacheKey);
  241. return string.IsNullOrWhiteSpace(res) ? null : JSON.Deserialize<List<AntMenuOutput>>(res);
  242. }
  243. /// <summary>
  244. /// 缓存菜单
  245. /// </summary>
  246. /// <param name="userId"></param>
  247. /// <param name="menus"></param>
  248. /// <returns></returns>
  249. public async Task SetMenu(int userId, List<AntMenuOutput> menus)
  250. {
  251. var cacheKey = $"{CommonConst.CACHE_KEY_MENU}{userId}";
  252. await _cache.SetStringAsync(cacheKey, JSON.Serialize(menus));
  253. await AddCacheKey(cacheKey);
  254. }
  255. }