InfoCache.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System.Timers;
  2. using Timer = System.Timers.Timer;
  3. namespace VberZero.Tools.CacheHelpers;
  4. /// <summary>
  5. /// 缓存
  6. /// </summary>
  7. public abstract class InfoCache<T> where T : class, new()
  8. {
  9. //public static InfoCache<T> Instance = new InfoCache<T>();
  10. protected InfoCache()
  11. {
  12. Timer = new Timer();
  13. ExpireInterval = 60 * 2;
  14. ClearInterval = 30;
  15. StartClear();
  16. }
  17. /// <summary>
  18. ///
  19. /// </summary>
  20. /// <param name="expireInterval">缓存过期时间(分钟)默认120分钟</param>
  21. /// <param name="clearInterval">缓存清理间隔(分钟)默认30分钟</param>
  22. protected InfoCache(int? expireInterval, int? clearInterval)
  23. {
  24. Timer = new Timer();
  25. ExpireInterval = expireInterval ?? 60 * 2;
  26. ClearInterval = clearInterval ?? 30;
  27. StartClear();
  28. }
  29. protected void StartClear()
  30. {
  31. StopClear();
  32. if (!HasStarted)
  33. {
  34. Timer.Enabled = true;
  35. Timer.Interval = 1000 * 60 * ClearInterval;
  36. Timer.Start();
  37. Timer.Elapsed += CheckOverTime;
  38. HasStarted = true;
  39. }
  40. }
  41. protected void StopClear()
  42. {
  43. if (HasStarted)
  44. {
  45. Timer.Stop();
  46. Timer.Elapsed -= CheckOverTime;
  47. HasStarted = false;
  48. }
  49. }
  50. public bool HasStarted;
  51. /// <summary>
  52. /// 缓存清理间隔(分钟)
  53. /// </summary>
  54. protected int ClearInterval { get; set; }
  55. /// <summary>
  56. /// 缓存过期时间(分钟)
  57. /// </summary>
  58. protected int ExpireInterval { get; set; }
  59. /// <summary>
  60. /// 尝试获取缓存
  61. /// </summary>
  62. /// <param name="key"></param>
  63. /// <param name="value"></param>
  64. /// <returns></returns>
  65. public virtual bool TryGetCache(string key, out T? value)
  66. {
  67. value = default(T);
  68. if (Caches.ContainsKey(key))
  69. {
  70. InfoWithDate valueWithDate = Caches[key];
  71. value = valueWithDate.GroupInfo;
  72. valueWithDate.ActiveTime = DateTime.Now;
  73. return true;
  74. }
  75. return false;
  76. }
  77. /// <summary>
  78. /// 添加缓存
  79. /// </summary>
  80. /// <param name="key"></param>
  81. /// <param name="value"></param>
  82. public virtual void AddCache(string key, T value)
  83. {
  84. if (Caches.ContainsKey(key))
  85. {
  86. Caches[key] = new InfoWithDate(value);
  87. }
  88. else
  89. {
  90. Caches.Add(key, new InfoWithDate(value));
  91. }
  92. }
  93. /// <summary>
  94. /// 移除缓存
  95. /// </summary>
  96. /// <param name="key"></param>
  97. public virtual void RemoveCache(string key)
  98. {
  99. if (Caches.ContainsKey(key))
  100. {
  101. Caches.Remove(key);
  102. }
  103. }
  104. /// <summary>
  105. /// 清除所有缓存信息
  106. /// </summary>
  107. public virtual void ClearCache()
  108. {
  109. Caches.Clear();
  110. }
  111. private Timer Timer { get; }
  112. private void CheckOverTime(object? source, ElapsedEventArgs e)
  113. {
  114. Dictionary<string, InfoWithDate> temp = new Dictionary<string, InfoWithDate>();
  115. foreach (var cache in Caches)
  116. {
  117. if (cache.Value.ActiveTime.CompareTo(DateTime.Now.AddMinutes(-ExpireInterval)) >= 0)
  118. {
  119. temp.Add(cache.Key, cache.Value);
  120. }
  121. }
  122. lock (Caches)
  123. {
  124. Caches = temp;
  125. }
  126. }
  127. private static Dictionary<string, InfoWithDate> Caches { get; set; } = new Dictionary<string, InfoWithDate>();
  128. private class InfoWithDate
  129. {
  130. public InfoWithDate(T? value)
  131. {
  132. ActiveTime = DateTime.Now;
  133. GroupInfo = value;
  134. }
  135. public DateTime ActiveTime { get; set; }
  136. public T? GroupInfo { get; }
  137. }
  138. }