using System; using System.Collections.Generic; using System.Diagnostics; using System.Threading; using LJLib.D; namespace LJLib { /// /// 缓存,超时自动清除,线程安全 /// public sealed class LJCache : Dictionary { /// /// 自动延长分钟数 /// public double DefaultAddMinutes { get { return _defaultAddMinutes;} set { _defaultAddMinutes = value; } } /// /// 添加键值对, /// /// /// /// 超时分钟数 public void Add(TKey key, TValue value, double minutes) { var deadline = DateTime.Now.AddMinutes(minutes); Add(key, value, deadline); } /// /// 添加永久对象或者按默认时间添加 /// /// /// public new void Add(TKey key, TValue value) { Add(key, value, _defaultAddMinutes); } public new TValue this[TKey key] { get { lock (syncRoot) { SetDeadLine(key, DateTime.Now.AddMinutes(DefaultAddMinutes)); return base[key]; } } set { lock (syncRoot) { SetDeadLine(key, DateTime.Now.AddMinutes(DefaultAddMinutes)); base[key] = value; } } } public new bool ContainsKey(TKey key) { lock (syncRoot) { var rslt = base.ContainsKey(key); if (rslt) { SetDeadLine(key, DateTime.Now.AddMinutes(DefaultAddMinutes)); } return rslt; } } #region 稀有成员实现逻辑 /// /// 添加键值对,设置超时时间 /// /// /// /// 超时时间 private void Add(TKey key, TValue value, DateTime deadline) { lock (syncRoot) { base[key] = value; SetDeadLine(key, deadline); } } /// /// 手动设置超时时间 /// /// /// /// private void SetDeadLine(TKey key, DateTime deadline) { lock (syncRoot) { deadline = deadline.AddMilliseconds(-deadline.Millisecond); if (_deadlines == null) { _deadlines = new Dictionary(); _ix_keys = new SortedList>(); } if (_deadlines.ContainsKey(key)) { var olddt = _deadlines[key]; _ix_keys[olddt].Remove(key); if (_ix_keys[olddt].Count == 0) { _ix_keys.Remove(olddt); } } _deadlines[key] = deadline; if (!_ix_keys.ContainsKey(deadline)) { _ix_keys[deadline] = new HashSet(); } if (!_ix_keys[deadline].Contains(key)) { _ix_keys[deadline].Add(key); } if (_timer == null) { _timer = new Timer((state) => { try { //DebugHelper.GetTotalMemory("LJCache.Remove.开始"); lock (syncRoot) { var dtNow = DateTime.Now; List dts = new List(); for (int i = 0; _ix_keys != null && i < _ix_keys.Keys.Count && _ix_keys.Keys[i] <= dtNow; i++) { dts.Add(_ix_keys.Keys[i]); foreach (var oldkey in _ix_keys.Values[i]) { _deadlines.Remove(oldkey); base.Remove(oldkey); } } foreach (var dt in dts) { _ix_keys.Remove(dt); } } //DebugHelper.GetTotalMemory("LJCache.Remove.结束"); } catch (Exception ex) { Trace.Write(ex); } }, null, 1000, 1000); } } } /// /// 线路同步对象 /// private object syncRoot = new object(); /// /// 缓存清除定时器 /// private Timer _timer; /// /// 各键值对的销毁时间 /// private Dictionary _deadlines; /// /// 按时间顺序过期 /// private SortedList> _ix_keys; /// /// 自动延长分钟数 /// private double _defaultAddMinutes = 5; public LJCache(IEqualityComparer comparer) : base(comparer) { } public LJCache() : base() { } #endregion } }