InfoCache.cs 4.2 KB

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