public void Insert(CacheEntry head) { Prev = head; Next = head.Next; Next.Prev = this; Prev.Next = this; }
public Cache(int size) { _size = size; _map = new Dictionary<object, CacheEntry>(size); _head = new CacheEntry(null, null); _tail = new CacheEntry(null, null); _head.Next = _tail; _tail.Prev = _head; }
public void Add(object key, object value) { if (_ejector == null) { throw new InvalidOperationException("You must set the Ejector strategy in order to use the cache."); } CacheEntry entry; if (_map.TryGetValue(key, out entry)) { entry.Remove(); entry.Insert(_head); entry.Value = value; } else { entry = new CacheEntry(key, value); _map.Add(key, entry); entry.Insert(_head); if (_map.Count > _size) { CacheEntry victim = _ejector.GetEjectionVictim(); _map.Remove(victim.Key); victim.Remove(); } } }