CacheHelper.cs 4.8 KB

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