CSRedisCacheService.cs 9.1 KB

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