集成,总结MES功能
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

164 lines
5.1 KiB

  1. using Furion.JsonSerialization;
  2. using Microsoft.Extensions.Caching.Distributed;
  3. using System.Text;
  4. namespace BPA.MES.Base.Application
  5. {
  6. /// <summary>
  7. /// 系统缓存服务
  8. /// </summary>
  9. [ApiDescriptionSettings(Name = "Cache", Order = 100)]
  10. public class SysCacheService : ISysCacheService, IDynamicApiController, ISingleton
  11. {
  12. private readonly IDistributedCache _cache;
  13. public SysCacheService(IDistributedCache cache)
  14. {
  15. _cache = cache;
  16. }
  17. /// <summary>
  18. /// 获取所有缓存关键字
  19. /// </summary>
  20. /// <returns></returns>
  21. [HttpGet("sysCache/keyList")]
  22. public async Task<List<string>> GetAllCacheKeys()
  23. {
  24. var res = await _cache.GetStringAsync(ClaimConst.CACHE_KEY_ALL);
  25. return string.IsNullOrWhiteSpace(res) ? null : JSON.Deserialize<List<string>>(res);
  26. }
  27. /// <summary>
  28. /// 删除指定关键字缓存
  29. /// </summary>
  30. /// <param name="key"></param>
  31. /// <returns></returns>
  32. [HttpGet("sysCache/remove")]
  33. public async Task RemoveAsync(string key)
  34. {
  35. await _cache.RemoveAsync(key);
  36. await DelCacheKey(key);
  37. }
  38. /// <summary>
  39. /// 删除某特征关键字缓存
  40. /// </summary>
  41. /// <param name="key"></param>
  42. /// <returns></returns>
  43. [NonAction]
  44. public async Task DelByPatternAsync(string key)
  45. {
  46. var allkeys = await GetAllCacheKeys();
  47. var delAllkeys = allkeys.Where(u => u.Contains(key)).ToList();
  48. // 删除相应的缓存
  49. delAllkeys.ForEach(u =>
  50. {
  51. _cache.Remove(u);
  52. });
  53. // 更新所有缓存键
  54. allkeys = allkeys.Where(u => !u.Contains(key)).ToList();
  55. await _cache.SetStringAsync(ClaimConst.CACHE_KEY_ALL, JSON.Serialize(allkeys));
  56. }
  57. /// <summary>
  58. /// 设置缓存
  59. /// </summary>
  60. /// <param name="cacheKey"></param>
  61. /// <param name="value"></param>
  62. /// <returns></returns>
  63. [NonAction]
  64. public async Task SetAsync(string cacheKey, object value)
  65. {
  66. await _cache.SetAsync(cacheKey, Encoding.UTF8.GetBytes(JSON.Serialize(value)));
  67. await AddCacheKey(cacheKey);
  68. }
  69. /// <summary>
  70. /// 设置缓存
  71. /// </summary>
  72. /// <param name="cacheKey"></param>
  73. /// <param name="value"></param>
  74. /// <returns></returns>
  75. [NonAction]
  76. public async Task SetStringAsync(string cacheKey, string value)
  77. {
  78. await _cache.SetStringAsync(cacheKey, value);
  79. await AddCacheKey(cacheKey);
  80. }
  81. /// <summary>
  82. /// 获取缓存
  83. /// </summary>
  84. /// <param name="cacheKey"></param>
  85. /// <returns></returns>
  86. [HttpGet("sysCache/detail")]
  87. public async Task<string> GetStringAsync(string cacheKey)
  88. {
  89. return await _cache.GetStringAsync(cacheKey);
  90. }
  91. /// <summary>
  92. /// 获取缓存
  93. /// </summary>
  94. /// <typeparam name="T"></typeparam>
  95. /// <param name="cacheKey"></param>
  96. /// <returns></returns>
  97. [NonAction]
  98. public async Task<T> GetAsync<T>(string cacheKey)
  99. {
  100. var res = await _cache.GetAsync(cacheKey);
  101. return res == null ? default : JSON.Deserialize<T>(Encoding.UTF8.GetString(res));
  102. }
  103. /// <summary>
  104. /// 检查给定 key 是否存在
  105. /// </summary>
  106. /// <param name="cacheKey">键</param>
  107. /// <returns></returns>
  108. [NonAction]
  109. public bool Exists(string cacheKey)
  110. {
  111. return _cache.Equals(cacheKey);
  112. }
  113. /// <summary>
  114. /// 增加缓存Key
  115. /// </summary>
  116. /// <param name="cacheKey"></param>
  117. /// <returns></returns>
  118. [NonAction]
  119. public async Task AddCacheKey(string cacheKey)
  120. {
  121. var res = await _cache.GetStringAsync(ClaimConst.CACHE_KEY_ALL);
  122. var allkeys = string.IsNullOrWhiteSpace(res) ? new List<string>() : JSON.Deserialize<List<string>>(res);
  123. if (!allkeys.Any(m => m == cacheKey))
  124. {
  125. allkeys.Add(cacheKey);
  126. await _cache.SetStringAsync(ClaimConst.CACHE_KEY_ALL, JSON.Serialize(allkeys));
  127. }
  128. }
  129. /// <summary>
  130. ///
  131. /// </summary>
  132. /// <param name="cacheKey"></param>
  133. /// <returns></returns>
  134. [NonAction]
  135. public async Task DelCacheKey(string cacheKey)
  136. {
  137. var res = await _cache.GetStringAsync(ClaimConst.CACHE_KEY_ALL);
  138. var allkeys = string.IsNullOrWhiteSpace(res) ? new List<string>() : JSON.Deserialize<List<string>>(res);
  139. if (allkeys.Any(m => m == cacheKey))
  140. {
  141. allkeys.Remove(cacheKey);
  142. await _cache.SetStringAsync(ClaimConst.CACHE_KEY_ALL, JSON.Serialize(allkeys));
  143. }
  144. }
  145. }
  146. }