平安校园
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

230 linhas
5.3 KiB

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