internal void Dump(TextWriter output) { int cnt = 0; for (LruCacheNode node = _lruHead; node != null; node = node.Next) { Console.WriteLine("{0} = {1} (used:{2} expire:{3})", node.Key, node.Value, node.UsedCount, node.ExpirationTime); if (node.Previous != null && node.Previous.Next != node) { throw new Exception("LRU assertion failed #1"); } if (node.Previous == null && node != _lruHead) { throw new Exception("LRU assertion failed #2"); } if (node.Next != null && node.Next.Previous != node) { throw new Exception("LRU assertion failed #3"); } if (node.Next == null && node != _lruTail) { throw new Exception("LRU assertion failed #4"); } cnt++; } if (cnt != _hash.Count) { throw new Exception("LRU assertion failed #5, was " + cnt + " should be " + _hash.Count); } Console.WriteLine(); }
private void RemoveNode(LruCacheNode node) { if (node.Previous != null) { node.Previous.Next = node.Next; } if (node.Next != null) { node.Next.Previous = node.Previous; } else { _lruTail = node.Previous; } if (node == _lruHead) { _lruHead = node.Next; } node.Previous = null; node.Next = null; _hash.Remove(node.Key); if (ItemRemoved != null) { ItemRemoved(this, new LruCacheEventArgs(node.Key, node.Value)); } }
public void FillSnapshotTable(DataSet dataSet, string tableName) { lock (this) { DataTable dt = dataSet.Tables.Add(tableName); dt.Columns.Add("LruPosition", typeof(int)); dt.Columns.Add("Class", typeof(string)); dt.Columns.Add("Key", typeof(string)); dt.Columns.Add("Value", typeof(string)); dt.Columns.Add("AddedTime", typeof(DateTime)); dt.Columns.Add("LastAccessTime", typeof(DateTime)); dt.Columns.Add("ExpirationTime", typeof(DateTime)); dt.Columns.Add("UsedCount", typeof(int)); int pos = 0; for (LruCacheNode node = _lruHead; node != null; node = node.Next) { DataRow row = dt.NewRow(); string className; string keyValue; SoodaCacheKey ck1 = node.Key as SoodaCacheKey; SoodaCachedCollection scc = node.Value as SoodaCachedCollection; if (ck1 != null) { className = ck1.ClassName; keyValue = Convert.ToString(ck1.KeyValue); } else if (scc != null) { className = scc.RootClassName; keyValue = scc.CollectionKey; } else { className = Convert.ToString(node.Key); keyValue = null; } row.ItemArray = new object[] { pos++, className, keyValue, Convert.ToString(node.Value), node.AddedTime, node.LastAccessTime, node.ExpirationTime, node.UsedCount }; dt.Rows.Add(row); } } }
public void Remove(object key) { lock (this) { LruCacheNode node = (LruCacheNode)_hash[key]; if (node != null) { RemoveNode(node); } } }
public void Clear() { lock (this) { //Dump("Before Clear()"); _hash.Clear(); if (ItemRemoved != null) { for (LruCacheNode n = _lruHead; n != null; n = n.Next) { ItemRemoved(this, new LruCacheEventArgs(n.Key, n.Value)); } } _lruHead = _lruTail = null; //Dump("After Clear()"); } }
public void Set(object key, object value, TimeSpan expirationTimeout, bool slidingExpiration) { lock (this) { //Dump("Before Set()"); CheckForPeriodicSweep(DateTime.Now); LruCacheNode node = (LruCacheNode)_hash[key]; if (node == null) { Add(key, value, expirationTimeout, slidingExpiration); } else { MoveToLruHead(node); node.Value = value; } //Dump("After Set()"); } }
public void Add(object key, object value, TimeSpan expirationTimeout, bool slidingExpiration) { lock (this) { DateTime now = DateTime.Now; CheckForPeriodicSweep(now); if (_maxItems != -1 && _hash.Count >= _maxItems) { RemoveLruItem(); //Dump("After RemoveLruItem"); } LruCacheNode node = new LruCacheNode(); node.AddedTime = now; node.SlidingExpiration = slidingExpiration; node.ExpirationTimeout = expirationTimeout; node.LastAccessTime = now; node.Previous = null; node.Next = _lruHead; node.Key = key; node.Value = value; node.ExpirationTime = now + expirationTimeout; if (_lruHead != null) { _lruHead.Previous = node; } _lruHead = node; if (_lruTail == null) { _lruTail = node; } _hash.Add(key, node); if (ItemAdded != null) { ItemAdded(this, new LruCacheEventArgs(node.Key, node.Value)); } //Dump("After Add()"); } }
public void Sweep() { lock (this) { DateTime now = DateTime.Now; //Dump("Before Sweep()"); for (LruCacheNode node = _lruHead; node != null; node = node.Next) { if (now >= node.ExpirationTime) { TrimBeforeNode(node); return; } } } //Dump("After Sweep()"); }
public object Get(object key) { lock (this) { DateTime now = DateTime.Now; CheckForPeriodicSweep(now); LruCacheNode node = (LruCacheNode)_hash[key]; if (node == null) { return(null); } if (now >= node.ExpirationTime) { TrimBeforeNode(node); return(null); } MoveToLruHead(node); return(node.Value); } }
private void MoveToLruHead(LruCacheNode node) { //Dump("Before MoveToLruHead(" + node.Key + ")"); if (ItemUsed != null) { ItemUsed(this, new LruCacheEventArgs(node.Key, node.Value)); } if (node != _lruHead) { node.Previous.Next = node.Next; if (node.Next != null) { node.Next.Previous = node.Previous; } else { _lruTail = node.Previous; } if (_lruHead != null) { _lruHead.Previous = node; } node.Next = _lruHead; node.Previous = null; _lruHead = node; } DateTime now = DateTime.Now; node.LastAccessTime = now; if (node.SlidingExpiration) { node.ExpirationTime = now + node.ExpirationTimeout; } node.UsedCount++; //Dump("After MoveToLruHead()"); }
private void TrimBeforeNode(LruCacheNode node) { if (node.Previous == null) { Clear(); return; } lock (this) { _lruTail = node.Previous; _lruTail.Next = null; for (LruCacheNode n = node; n != null; n = n.Next) { _hash.Remove(n.Key); if (ItemRemoved != null) { ItemRemoved(this, new LruCacheEventArgs(n.Key, n.Value)); } } } //Dump("After TrimBeforeNode()"); }
public void Add(object key, object value, TimeSpan expirationTimeout, bool slidingExpiration) { lock (this) { DateTime now = DateTime.Now; CheckForPeriodicSweep(now); if (_maxItems != -1 && _hash.Count >= _maxItems) { RemoveLruItem(); //Dump("After RemoveLruItem"); } LruCacheNode node = new LruCacheNode(); node.AddedTime = now; node.SlidingExpiration = slidingExpiration; node.ExpirationTimeout = expirationTimeout; node.LastAccessTime = now; node.Previous = null; node.Next = _lruHead; node.Key = key; node.Value = value; node.ExpirationTime = now + expirationTimeout; if (_lruHead != null) { _lruHead.Previous = node; } _lruHead = node; if (_lruTail == null) _lruTail = node; _hash.Add(key, node); if (ItemAdded != null) { ItemAdded(this, new LruCacheEventArgs(node.Key, node.Value)); } //Dump("After Add()"); } }
private void RemoveNode(LruCacheNode node) { if (node.Previous != null) node.Previous.Next = node.Next; if (node.Next != null) { node.Next.Previous = node.Previous; } else { _lruTail = node.Previous; } if (node == _lruHead) _lruHead = node.Next; node.Previous = null; node.Next = null; _hash.Remove(node.Key); if (ItemRemoved != null) { ItemRemoved(this, new LruCacheEventArgs(node.Key, node.Value)); } }
private void MoveToLruHead(LruCacheNode node) { //Dump("Before MoveToLruHead(" + node.Key + ")"); if (ItemUsed != null) { ItemUsed(this, new LruCacheEventArgs(node.Key, node.Value)); } if (node != _lruHead) { node.Previous.Next = node.Next; if (node.Next != null) { node.Next.Previous = node.Previous; } else { _lruTail = node.Previous; } if (_lruHead != null) { _lruHead.Previous = node; } node.Next = _lruHead; node.Previous = null; _lruHead = node; } DateTime now = DateTime.Now; node.LastAccessTime = now; if (node.SlidingExpiration) node.ExpirationTime = now + node.ExpirationTimeout; node.UsedCount++; //Dump("After MoveToLruHead()"); }