CSRedisCacheService.cs 9.0 KB

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