MemoryCacheService.cs 5.2 KB

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