MemoryCacheService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using Microsoft.Extensions.Caching.Memory;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace TEAMModelOS.SDK.Module.Cache
  8. {
  9. public class MemoryCacheService : ICacheService ,IDisposable
  10. {
  11. protected IMemoryCache _cache;
  12. public MemoryCacheService(IMemoryCache cache)
  13. {
  14. _cache = cache;
  15. }/// <summary>
  16. /// 验证缓存项是否存在
  17. /// </summary>
  18. /// <param name="key">缓存Key</param>
  19. /// <returns></returns>
  20. public bool Exists(string key)
  21. {
  22. if (key == null)
  23. {
  24. throw new ArgumentNullException(nameof(key));
  25. }
  26. object cached;
  27. return _cache.TryGetValue(key, out cached);
  28. }/// <summary>
  29. /// 添加缓存
  30. /// </summary>
  31. /// <param name="key">缓存Key</param>
  32. /// <param name="value">缓存Value</param>
  33. /// <returns></returns>
  34. public bool Add(string key, object value)
  35. {
  36. if (key == null)
  37. {
  38. throw new ArgumentNullException(nameof(key));
  39. }
  40. if (value == null)
  41. {
  42. throw new ArgumentNullException(nameof(value));
  43. }
  44. _cache.Set(key, value);
  45. return Exists(key);
  46. }
  47. /// <summary>
  48. /// 添加缓存
  49. /// </summary>
  50. /// <param name="key">缓存Key</param>
  51. /// <param name="value">缓存Value</param>
  52. /// <param name="expiresSliding">滑动过期时长(如果在过期时间内有操作,则以当前时间点延长过期时间)</param>
  53. /// <param name="expiressAbsoulte">绝对过期时长</param>
  54. /// <returns></returns>
  55. public bool Add(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte)
  56. {
  57. if (key == null)
  58. {
  59. throw new ArgumentNullException(nameof(key));
  60. }
  61. if (value == null)
  62. {
  63. throw new ArgumentNullException(nameof(value));
  64. }
  65. _cache.Set(key, value,
  66. new MemoryCacheEntryOptions()
  67. .SetSlidingExpiration(expiresSliding)
  68. .SetAbsoluteExpiration(expiressAbsoulte)
  69. );
  70. return Exists(key);
  71. }
  72. /// <summary>
  73. /// 添加缓存
  74. /// </summary>
  75. /// <param name="key">缓存Key</param>
  76. /// <param name="value">缓存Value</param>
  77. /// <param name="expiresIn">缓存时长</param>
  78. /// <param name="isSliding">是否滑动过期(如果在过期时间内有操作,则以当前时间点延长过期时间)</param>
  79. /// <returns></returns>
  80. public bool Add(string key, object value, TimeSpan expiresIn, bool isSliding = false)
  81. {
  82. if (key == null)
  83. {
  84. throw new ArgumentNullException(nameof(key));
  85. }
  86. if (value == null)
  87. {
  88. throw new ArgumentNullException(nameof(value));
  89. }
  90. if (isSliding)
  91. _cache.Set(key, value,
  92. new MemoryCacheEntryOptions()
  93. .SetSlidingExpiration(expiresIn)
  94. );
  95. else
  96. _cache.Set(key, value,
  97. new MemoryCacheEntryOptions()
  98. .SetAbsoluteExpiration(expiresIn)
  99. );
  100. return Exists(key);
  101. }/// <summary>
  102. /// 删除缓存
  103. /// </summary>
  104. /// <param name="key">缓存Key</param>
  105. /// <returns></returns>
  106. public bool Remove(string key)
  107. {
  108. if (key == null)
  109. {
  110. throw new ArgumentNullException(nameof(key));
  111. }
  112. _cache.Remove(key);
  113. return !Exists(key);
  114. }
  115. /// <summary>
  116. /// 批量删除缓存
  117. /// </summary>
  118. /// <param name="key">缓存Key集合</param>
  119. /// <returns></returns>
  120. public void RemoveAll(IEnumerable<string> keys)
  121. {
  122. if (keys == null)
  123. {
  124. throw new ArgumentNullException(nameof(keys));
  125. }
  126. keys.ToList().ForEach(item => _cache.Remove(item));
  127. }/// <summary>
  128. /// 获取缓存
  129. /// </summary>
  130. /// <param name="key">缓存Key</param>
  131. /// <returns></returns>
  132. public T Get<T>(string key) where T : class
  133. {
  134. if (key == null)
  135. {
  136. throw new ArgumentNullException(nameof(key));
  137. }
  138. return _cache.Get(key) as T;
  139. }
  140. /// <summary>
  141. /// 获取缓存
  142. /// </summary>
  143. /// <param name="key">缓存Key</param>
  144. /// <returns></returns>
  145. public object Get(string key)
  146. {
  147. if (key == null)
  148. {
  149. throw new ArgumentNullException(nameof(key));
  150. }
  151. return _cache.Get(key);
  152. }
  153. /// <summary>
  154. /// 获取缓存集合
  155. /// </summary>
  156. /// <param name="keys">缓存Key集合</param>
  157. /// <returns></returns>
  158. public IDictionary<string, object> GetAll(IEnumerable<string> keys)
  159. {
  160. if (keys == null)
  161. {
  162. throw new ArgumentNullException(nameof(keys));
  163. }
  164. var dict = new Dictionary<string, object>();
  165. keys.ToList().ForEach(item => dict.Add(item, _cache.Get(item)));
  166. return dict;
  167. }/// <summary>
  168. /// 修改缓存
  169. /// </summary>
  170. /// <param name="key">缓存Key</param>
  171. /// <param name="value">新的缓存Value</param>
  172. /// <returns></returns>
  173. public bool Replace(string key, object value)
  174. {
  175. if (key == null)
  176. {
  177. throw new ArgumentNullException(nameof(key));
  178. }
  179. if (value == null)
  180. {
  181. throw new ArgumentNullException(nameof(value));
  182. }
  183. if (Exists(key))
  184. if (!Remove(key)) return false;
  185. return Add(key, value);
  186. }
  187. /// <summary>
  188. /// 修改缓存
  189. /// </summary>
  190. /// <param name="key">缓存Key</param>
  191. /// <param name="value">新的缓存Value</param>
  192. /// <param name="expiresSliding">滑动过期时长(如果在过期时间内有操作,则以当前时间点延长过期时间)</param>
  193. /// <param name="expiressAbsoulte">绝对过期时长</param>
  194. /// <returns></returns>
  195. public bool Replace(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte)
  196. {
  197. if (key == null)
  198. {
  199. throw new ArgumentNullException(nameof(key));
  200. }
  201. if (value == null)
  202. {
  203. throw new ArgumentNullException(nameof(value));
  204. }
  205. if (Exists(key))
  206. if (!Remove(key)) return false;
  207. return Add(key, value, expiresSliding, expiressAbsoulte);
  208. }
  209. /// <summary>
  210. /// 修改缓存
  211. /// </summary>
  212. /// <param name="key">缓存Key</param>
  213. /// <param name="value">新的缓存Value</param>
  214. /// <param name="expiresIn">缓存时长</param>
  215. /// <param name="isSliding">是否滑动过期(如果在过期时间内有操作,则以当前时间点延长过期时间)</param>
  216. /// <returns></returns>
  217. public bool Replace(string key, object value, TimeSpan expiresIn, bool isSliding = false)
  218. {
  219. if (key == null)
  220. {
  221. throw new ArgumentNullException(nameof(key));
  222. }
  223. if (value == null)
  224. {
  225. throw new ArgumentNullException(nameof(value));
  226. }
  227. if (Exists(key))
  228. if (!Remove(key)) return false;
  229. return Add(key, value, expiresIn, isSliding);
  230. }
  231. public void Dispose()
  232. {
  233. if (_cache != null)
  234. _cache.Dispose();
  235. GC.SuppressFinalize(this);
  236. }
  237. public async Task<bool> ExistsAsync(string key)
  238. {
  239. return await Task.Run(() => Exists(key));
  240. }
  241. public async Task<bool> AddAsync(string key, object value)
  242. {
  243. return await Task.Run(() => Add(key, value));
  244. }
  245. public async Task<bool> AddAsync(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte)
  246. {
  247. return await Task.Run(() => Add(key, value, expiresSliding, expiressAbsoulte));
  248. }
  249. public async Task<bool> AddAsync(string key, object value, TimeSpan expiresIn, bool isSliding = false)
  250. {
  251. return await Task.Run(() => Add(key, value, expiresIn, isSliding));
  252. }
  253. public async Task<bool> RemoveAsync(string key)
  254. {
  255. return await Task.Run(() => Remove(key));
  256. }
  257. public async Task RemoveAllAsync(IEnumerable<string> keys)
  258. {
  259. await Task.Run(() => RemoveAll(keys));
  260. }
  261. public async Task<T> GetAsync<T>(string key) where T : class
  262. {
  263. return await Task.Run(() => Get<T>(key));
  264. }
  265. public async Task<object> GetAsync(string key)
  266. {
  267. return await Task.Run(() => Get(key));
  268. }
  269. public async Task<IDictionary<string, object>> GetAllAsync(IEnumerable<string> keys)
  270. {
  271. return await Task.Run(() => GetAll(keys));
  272. }
  273. public async Task<bool> ReplaceAsync(string key, object value)
  274. {
  275. return await Task.Run(() => Replace(key,value));
  276. }
  277. public async Task<bool> ReplaceAsync(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte)
  278. {
  279. return await Task.Run(() => Replace(key, value, expiresSliding, expiressAbsoulte));
  280. }
  281. public async Task<bool> ReplaceAsync(string key, object value, TimeSpan expiresIn, bool isSliding = false)
  282. {
  283. return await Task.Run(() => Replace(key, value, expiresIn, isSliding));
  284. }
  285. }
  286. }