| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- using System.Timers;
- using Timer = System.Timers.Timer;
- namespace VberZero.Tools.CacheHelpers;
- /// <summary>
- /// 缓存
- /// </summary>
- public abstract class InfoCache<T> where T : class, new()
- {
- //public static InfoCache<T> Instance = new InfoCache<T>();
- protected InfoCache()
- {
- Timer = new Timer();
- ExpireInterval = 60 * 2;
- ClearInterval = 30;
- StartClear();
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="expireInterval">缓存过期时间(分钟)默认120分钟</param>
- /// <param name="clearInterval">缓存清理间隔(分钟)默认30分钟</param>
- protected InfoCache(int? expireInterval, int? clearInterval)
- {
- Timer = new Timer();
- ExpireInterval = expireInterval ?? 60 * 2;
- ClearInterval = clearInterval ?? 30;
- StartClear();
- }
- protected void StartClear()
- {
- StopClear();
- if (!HasStarted)
- {
- Timer.Enabled = true;
- Timer.Interval = 1000 * 60 * ClearInterval;
- Timer.Start();
- Timer.Elapsed += CheckOverTime;
- HasStarted = true;
- }
- }
- protected void StopClear()
- {
- if (HasStarted)
- {
- Timer.Stop();
- Timer.Elapsed -= CheckOverTime;
- HasStarted = false;
- }
- }
- public bool HasStarted;
- /// <summary>
- /// 缓存清理间隔(分钟)
- /// </summary>
- protected int ClearInterval { get; set; }
- /// <summary>
- /// 缓存过期时间(分钟)
- /// </summary>
- protected int ExpireInterval { get; set; }
- /// <summary>
- /// 尝试获取缓存
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public virtual bool TryGetCache(string key, out T? value)
- {
- value = default(T);
- if (Caches.ContainsKey(key))
- {
- InfoWithDate valueWithDate = Caches[key];
- value = valueWithDate.GroupInfo;
- valueWithDate.ActiveTime = DateTime.Now;
- return true;
- }
- return false;
- }
- /// <summary>
- /// 添加缓存
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- public virtual void AddCache(string key, T value)
- {
- if (Caches.ContainsKey(key))
- {
- Caches[key] = new InfoWithDate(value);
- }
- else
- {
- Caches.Add(key, new InfoWithDate(value));
- }
- }
- /// <summary>
- /// 移除缓存
- /// </summary>
- /// <param name="key"></param>
- public virtual void RemoveCache(string key)
- {
- if (Caches.ContainsKey(key))
- {
- Caches.Remove(key);
- }
- }
- /// <summary>
- /// 清除所有缓存信息
- /// </summary>
- public virtual void ClearCache()
- {
- Caches.Clear();
- }
- private Timer Timer { get; }
- private void CheckOverTime(object? source, ElapsedEventArgs e)
- {
- Dictionary<string, InfoWithDate> temp = new Dictionary<string, InfoWithDate>();
- foreach (var cache in Caches)
- {
- if (cache.Value.ActiveTime.CompareTo(DateTime.Now.AddMinutes(-ExpireInterval)) >= 0)
- {
- temp.Add(cache.Key, cache.Value);
- }
- }
- lock (Caches)
- {
- Caches = temp;
- }
- }
- private static Dictionary<string, InfoWithDate> Caches { get; set; } = new Dictionary<string, InfoWithDate>();
- private class InfoWithDate
- {
- public InfoWithDate(T? value)
- {
- ActiveTime = DateTime.Now;
- GroupInfo = value;
- }
- public DateTime ActiveTime { get; set; }
- public T? GroupInfo { get; }
- }
- }
|