平安校园
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

MemoryCacheService.cs 5.3 KiB

3 месяцев назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. 
  2. //
  3. namespace SafeCampus.Cache;
  4. /// <summary>
  5. /// <inheritdoc cref="ISimpleCacheService"/>
  6. /// 内存缓存
  7. /// </summary>
  8. public partial class MemoryCacheService : ISimpleCacheService
  9. {
  10. private readonly MemoryCache _memoryCache;
  11. public MemoryCacheService()
  12. {
  13. _memoryCache = new MemoryCache();
  14. }
  15. #region 普通操作
  16. /// <inheritdoc/>
  17. public T Get<T>(string key)
  18. {
  19. var data = _memoryCache.Get<string>(key);
  20. return data.ToObject<T>();
  21. }
  22. /// <inheritdoc/>
  23. public int Remove(params string[] keys)
  24. {
  25. return _memoryCache.Remove(keys);
  26. }
  27. /// <inheritdoc/>
  28. public bool Set<T>(string key, T value, int expire = -1)
  29. {
  30. return _memoryCache.Set(key, value.ToJson(), expire);
  31. }
  32. /// <inheritdoc/>
  33. public bool Set<T>(string key, T value, TimeSpan expire)
  34. {
  35. return _memoryCache.Set(key, value.ToJson(), expire);
  36. }
  37. /// <inheritdoc/>
  38. public bool SetExpire(string key, TimeSpan expire)
  39. {
  40. return _memoryCache.SetExpire(key, expire);
  41. }
  42. /// <inheritdoc/>
  43. public TimeSpan GetExpire(string key)
  44. {
  45. return _memoryCache.GetExpire(key);
  46. }
  47. /// <inheritdoc/>
  48. public bool ContainsKey(string key)
  49. {
  50. return _memoryCache.ContainsKey(key);
  51. }
  52. /// <inheritdoc/>
  53. public void Clear()
  54. {
  55. _memoryCache.Clear();
  56. }
  57. /// <inheritdoc/>
  58. public void DelByPattern(string pattern)
  59. {
  60. var keys = _memoryCache.Keys.ToList();//获取所有key
  61. keys.ForEach(it =>
  62. {
  63. if (it.Contains(pattern))//如果匹配
  64. _memoryCache.Remove(pattern);
  65. });
  66. }
  67. #endregion 普通操作
  68. #region 集合操作
  69. /// <inheritdoc/>
  70. public IDictionary<string, T> GetAll<T>(IEnumerable<string> keys)
  71. {
  72. IDictionary<string, T>? result = default;//定义集合
  73. var data = _memoryCache.GetAll<string>(keys);//获取数据
  74. data.ForEach(it =>
  75. {
  76. result.Add(it.Key, it.Value.ToObject<T>());//遍历数据,格式化并加到新的数据集合
  77. });
  78. return result;
  79. }
  80. /// <inheritdoc/>
  81. public void SetAll<T>(IDictionary<string, T> values, int expire = -1)
  82. {
  83. IDictionary<string, string>? result = default;//定义集合
  84. values.ForEach(it =>
  85. {
  86. result.Add(it.Key, it.Value.ToJson());//遍历数据,格式化并加到新的数据集合
  87. });
  88. _memoryCache.SetAll(values, expire);
  89. }
  90. /// <inheritdoc/>
  91. public IDictionary<string, T> GetDictionary<T>(string key)
  92. {
  93. IDictionary<string, T>? result = default;//定义集合
  94. var data = _memoryCache.GetDictionary<string>(key);
  95. data.ForEach(it =>
  96. {
  97. result.Add(it.Key, it.Value.ToObject<T>());//遍历数据,格式化并加到新的数据集合
  98. });
  99. return result;
  100. }
  101. /// <inheritdoc/>
  102. public IProducerConsumer<T> GetQueue<T>(string key)
  103. {
  104. return _memoryCache.GetQueue<T>(key);
  105. }
  106. /// <inheritdoc/>
  107. public IProducerConsumer<T> GetStack<T>(string key)
  108. {
  109. return _memoryCache.GetStack<T>(key);
  110. }
  111. /// <inheritdoc/>
  112. public ICollection<T> GetSet<T>(string key)
  113. {
  114. return _memoryCache.GetSet<T>(key);
  115. }
  116. #endregion 集合操作
  117. #region 高级操作
  118. /// <inheritdoc/>
  119. public bool Add<T>(string key, T value, int expire = -1)
  120. {
  121. return _memoryCache.Add(key, value.ToJson(), expire);
  122. }
  123. /// <inheritdoc/>
  124. public IList<T> GetList<T>(string key)
  125. {
  126. IList<T> result = default;//定义集合
  127. var data = _memoryCache.GetList<string>(key);
  128. data.ForEach(it =>
  129. {
  130. result.Add(it.ToObject<T>());//遍历数据,格式化并加到新的数据集合
  131. });
  132. return result;
  133. }
  134. /// <inheritdoc/>
  135. public T Replace<T>(string key, T value)
  136. {
  137. return _memoryCache.Replace(key, value);
  138. }
  139. /// <inheritdoc/>
  140. public bool TryGetValue<T>(string key, out T value)
  141. {
  142. _ = _memoryCache.TryGetValue<string>(key, out var result);
  143. value = result.ToObject<T>();
  144. return value == null;
  145. }
  146. /// <inheritdoc/>
  147. public long Decrement(string key, long value)
  148. {
  149. return _memoryCache.Decrement(key, value);
  150. }
  151. /// <inheritdoc/>
  152. public double Decrement(string key, double value)
  153. {
  154. return _memoryCache.Decrement(key, value);
  155. }
  156. /// <inheritdoc/>
  157. public long Increment(string key, long value)
  158. {
  159. return _memoryCache.Increment(key, value);
  160. }
  161. /// <inheritdoc/>
  162. public double Increment(string key, double value)
  163. {
  164. return _memoryCache.Increment(key, value);
  165. }
  166. #endregion 高级操作
  167. #region 事务
  168. /// <inheritdoc/>
  169. public int Commit()
  170. {
  171. return _memoryCache.Commit();
  172. }
  173. /// <inheritdoc/>
  174. public IDisposable AcquireLock(string key, int msTimeout)
  175. {
  176. return _memoryCache.AcquireLock(key, msTimeout);
  177. }
  178. /// <inheritdoc/>
  179. public IDisposable AcquireLock(string key, int msTimeout, int msExpire,
  180. bool throwOnFailure)
  181. {
  182. return _memoryCache.AcquireLock(key, msTimeout, msExpire, throwOnFailure);
  183. }
  184. #endregion 事务
  185. }