RedisCacheService.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Text;
  3. using Microsoft.Extensions.Caching.Distributed;
  4. using Newtonsoft.Json;
  5. namespace HaBookCms.RedisStorage.Cache
  6. {
  7. public class RedisCacheService : ICacheService
  8. {
  9. private readonly IDistributedCache _cache;
  10. public RedisCacheService(IDistributedCache cache)
  11. {
  12. _cache = cache;
  13. }
  14. /// <summary>
  15. /// 是否存在此缓存
  16. /// </summary>
  17. /// <param name="key"></param>
  18. /// <returns></returns>
  19. public bool Exists(string key)
  20. {
  21. if (string.IsNullOrWhiteSpace(key))
  22. throw new ArgumentNullException(nameof(key));
  23. return !string.IsNullOrEmpty(_cache.GetString(key));
  24. }
  25. /// <summary>
  26.         /// 取得缓存数据
  27.         /// </summary>
  28.         /// <typeparam name="T"></typeparam>
  29.         /// <param name="key"></param>
  30.         /// <returns></returns>
  31.         public T GetCache<T>(string key) where T : class
  32. {
  33. if (string.IsNullOrWhiteSpace(key))
  34. throw new ArgumentNullException(nameof(key));
  35. var cacheVal = _cache.GetString(key);
  36. return JsonConvert.DeserializeObject<T>(cacheVal);
  37. }
  38.         /// <summary>
  39.         /// 设置缓存
  40.         /// </summary>
  41.         /// <param name="key"></param>
  42.         /// <param name="value"></param>
  43.         public void SetCache(string key, object value)
  44. {
  45. if (string.IsNullOrWhiteSpace(key))
  46. throw new ArgumentNullException(nameof(key));
  47. if (value == null)
  48. throw new ArgumentNullException(nameof(value));
  49. DistributedCacheEntryOptions options = new DistributedCacheEntryOptions
  50. {
  51. //设置过期默认为一个月
  52. AbsoluteExpiration = DateTime.Now.AddMonths(1)
  53. };
  54. // 添加缓存
  55. _cache.Set(key, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(value)), options);
  56. //刷新缓存
  57. _cache.Refresh(key);
  58. }
  59.         /// <summary>
  60.         /// 设置缓存,绝对过期 分钟
  61.         /// </summary>
  62.         /// <param name="key"></param>
  63.         /// <param name="value"></param>
  64.         /// <param name="expirationMinute">滑动过期时间</param>
  65.         /// MemoryCacheService.Default.SetCache("test", "RedisCache works!", 30);
  66.         public void SetCache(string key, object value, double expirationMinute)
  67. {
  68. if (string.IsNullOrWhiteSpace(key))
  69. throw new ArgumentNullException(nameof(key));
  70. if (value == null)
  71. throw new ArgumentNullException(nameof(value));
  72. DistributedCacheEntryOptions options = new DistributedCacheEntryOptions
  73. {
  74. //设置过期-间隔分钟
  75. AbsoluteExpiration = DateTime.Now.AddMinutes(expirationMinute)
  76. };
  77. // 添加缓存
  78. _cache.Set(key, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(value)), options);
  79. //刷新缓存
  80. _cache.Refresh(key);
  81. }
  82.         /// <summary>
  83.         /// 设置缓存,绝对过期
  84.         /// </summary>
  85.         /// <param name="key"></param>
  86.         /// <param name="value"></param>
  87.         /// <param name="expirationTime">DateTimeOffset 结束时间</param>
  88.         /// MemoryCacheService.Default.SetCache("test", "RedisCache works!", DateTimeOffset.Now.AddSeconds(30));
  89.         public void SetCache(string key, object value, DateTimeOffset expirationTime)
  90. {
  91. if (string.IsNullOrWhiteSpace(key))
  92. throw new ArgumentNullException(nameof(key));
  93. if (value == null)
  94. throw new ArgumentNullException(nameof(value));
  95. DistributedCacheEntryOptions options = new DistributedCacheEntryOptions
  96. {
  97. //设置过期-时间
  98. AbsoluteExpiration = expirationTime
  99. };
  100. // 添加缓存
  101. _cache.Set(key, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(value)), options);
  102. //刷新缓存
  103. _cache.Refresh(key);
  104. }
  105.         /// <summary>
  106.         /// 设置缓存,相对过期时间
  107.         /// </summary>
  108.         /// <param name="key"></param>
  109.         /// <param name="value"></param>
  110.         /// <param name="t"></param>
  111.         /// MemoryCacheService.Default.SetCache("test", "MemoryCache works!",TimeSpan.FromSeconds(30));
  112.         public void SetSlidingCache(string key, object value, TimeSpan t)
  113. {
  114. if (string.IsNullOrWhiteSpace(key))
  115. throw new ArgumentNullException(nameof(key));
  116. if (value == null)
  117. throw new ArgumentNullException(nameof(value));
  118. DistributedCacheEntryOptions options = new DistributedCacheEntryOptions
  119. {
  120. //设置过期-间隔分钟
  121. SlidingExpiration = t
  122. };
  123. // 添加缓存
  124. _cache.Set(key, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(value)), options);
  125. //刷新缓存
  126. _cache.Refresh(key);
  127. }
  128.         /// <summary>
  129.         /// 移除缓存
  130.         /// </summary>
  131.         /// <param name="key"></param>
  132.         public void RemoveCache(string key)
  133. {
  134. if (string.IsNullOrWhiteSpace(key))
  135. throw new ArgumentNullException(nameof(key));
  136. _cache.Remove(key);
  137. }
  138.         /// <summary>
  139.         /// 释放
  140.         /// </summary>
  141.         public void Dispose()
  142. {
  143. GC.SuppressFinalize(this);
  144. }
  145. }
  146. }