CacheHelper.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using Microsoft.Extensions.Caching.Memory;
  2. using System;
  3. using System.Collections.Generic;
  4. //不改命名空间,要修改很多地方
  5. namespace ZR.Common
  6. {
  7. public class CacheHelper
  8. {
  9. public static MemoryCache Cache { get; set; }
  10. private static readonly List<string> _keys;
  11. static CacheHelper()
  12. {
  13. Cache = new MemoryCache(new MemoryCacheOptions
  14. {
  15. //SizeLimit = 1024
  16. });
  17. _keys = [];
  18. }
  19. /// <summary>
  20. /// 获取缓存
  21. /// </summary>
  22. /// <typeparam name="T"></typeparam>
  23. /// <param name="key"></param>
  24. /// <returns></returns>
  25. public static T GetCache<T>(string key) where T : class
  26. {
  27. if (key == null)
  28. throw new ArgumentNullException(nameof(key));
  29. //return Cache.Get(key) as T; //或者
  30. return Cache.Get<T>(key);
  31. }
  32. /// <summary>
  33. /// 获取缓存
  34. /// </summary>
  35. /// <param name="CacheKey"></param>
  36. /// <returns></returns>
  37. public static object GetCache(string CacheKey)
  38. {
  39. return Cache.Get<object>(CacheKey);
  40. }
  41. public static object Get(string CacheKey)
  42. {
  43. return Cache.Get(CacheKey);
  44. }
  45. /// <summary>
  46. /// 设置缓存,永久缓存
  47. /// </summary>
  48. /// <param name="CacheKey">key</param>
  49. /// <param name="objObject">值</param>
  50. public static object SetCache(string CacheKey, object objObject)
  51. {
  52. return Cache.Set(CacheKey, objObject);
  53. }
  54. /// <summary>
  55. /// 设置缓存
  56. /// </summary>
  57. /// <param name="CacheKey">key</param>
  58. /// <param name="objObject">值</param>
  59. /// <param name="Timeout">过期时间(分钟)</param>
  60. public static object SetCache(string CacheKey, object objObject, int Timeout)
  61. {
  62. return Cache.Set(CacheKey, objObject, DateTime.Now.AddMinutes(Timeout));
  63. }
  64. /// <summary>
  65. /// 设置缓存(秒)
  66. /// </summary>
  67. /// <param name="CacheKey">key</param>
  68. /// <param name="objObject">值</param>
  69. /// <param name="Timeout">过期时间(秒)</param>
  70. public static void SetCaches(string CacheKey, object objObject, int Timeout)
  71. {
  72. if (!_keys.Contains(CacheKey))
  73. {
  74. _keys.Add(CacheKey);
  75. }
  76. Cache.Set(CacheKey, objObject, DateTime.Now.AddSeconds(Timeout));
  77. }
  78. /// <summary>
  79. /// 设置缓存
  80. /// </summary>
  81. /// <param name="CacheKey">key</param>
  82. /// <param name="objObject">值</param>
  83. /// <param name="absoluteExpiration">过期时间</param>
  84. /// <param name="slidingExpiration">过期时间间隔</param>
  85. public static object SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
  86. {
  87. return Cache.Set(CacheKey, objObject, absoluteExpiration);
  88. }
  89. /// <summary>
  90. /// 设定绝对的过期时间
  91. /// </summary>
  92. /// <param name="CacheKey"></param>
  93. /// <param name="objObject"></param>
  94. /// <param name="Seconds">超过多少秒后过期</param>
  95. public static void SetCacheDateTime(string CacheKey, object objObject, long Seconds)
  96. {
  97. Cache.Set(CacheKey, objObject, DateTime.Now.AddSeconds(Seconds));
  98. }
  99. /// <summary>
  100. /// 删除缓存
  101. /// </summary>
  102. /// <param name="key">key</param>
  103. public static void Remove(string key)
  104. {
  105. _keys.Remove(key);
  106. Cache.Remove(key);
  107. }
  108. /// <summary>
  109. /// 验证缓存项是否存在
  110. /// </summary>
  111. /// <param name="key">缓存Key</param>
  112. /// <returns></returns>
  113. public static bool Exists(string key)
  114. {
  115. if (key == null)
  116. throw new ArgumentNullException(nameof(key));
  117. return Cache.TryGetValue(key, out _);
  118. }
  119. /// <summary>
  120. /// 获取所有缓存键
  121. /// </summary>
  122. /// <returns></returns>
  123. //public static List<string> GetCacheKeys()
  124. //{
  125. // const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
  126. // //var entries = Cache.GetType().GetField("_entries", flags).GetValue(Cache);
  127. // //.net7需要这样写
  128. // var coherentState = Cache.GetType().GetField("_coherentState", flags).GetValue(Cache);
  129. // var entries = coherentState.GetType().GetField("_entries", flags).GetValue(coherentState);
  130. // var keys = new List<string>();
  131. // if (entries is not IDictionary cacheItems) return keys;
  132. // foreach (DictionaryEntry cacheItem in cacheItems)
  133. // {
  134. // keys.Add(cacheItem.Key.ToString());
  135. // //Console.WriteLine("缓存key=" +cacheItem.Key);
  136. // }
  137. // return keys;
  138. //}
  139. public static List<string> GetCacheKeys()
  140. {
  141. return new List<string>(_keys);
  142. }
  143. // 销毁缓存
  144. public void Dispose()
  145. {
  146. Cache.Dispose();
  147. }
  148. }
  149. }