public CacheItem <V> Remove(K key) { CacheItem <V> i = new CacheItem <V>(); if (_cacheDic.ContainsKey(key) && (_timerDic.ContainsKey(key))) { var cacheTimer = _timerDic[key]; _cacheDic.TryRemove(key, out i); cacheTimer.Elapsed -= new ElapsedEventHandler(Elapsed_Event); cacheTimer.Enabled = false; cacheTimer.AutoReset = false; cacheTimer.Stop(); cacheTimer.Close(); cacheTimer.Dispose(); if (DormantCacheCount == 0 && ActiveCount == 0) { EmptyCacheEvent?.Invoke(this, new EventArgs()); } } else { var dic = ReadBinary(); if (dic.ContainsKey(key)) { if (dic.TryRemove(key, out i)) { WriteBinary(dic); } } } return(i); }
public void Clear() { _cacheDic.Clear(); _timerDic.Clear(); _cleaner.Stop(); _cleaner.Start(); WriteCache(); EmptyCacheEvent?.Invoke(this, new EventArgs()); }
private void Cleaner_Event(object sender, ElapsedEventArgs e) { System.Threading.Thread cleaner = new System.Threading.Thread(() => { var dic = ReadBinary(); WriteBinary(dic); if (_dormantCacheCount == 0 && ActiveCount == 0) { EmptyCacheEvent?.Invoke(this, new EventArgs()); } }); cleaner.Start(); }