Ejemplo n.º 1
0
 public void Insert(CacheEntry head)
 {
     Prev = head;
     Next = head.Next;
     Next.Prev = this;
     Prev.Next = this;
 }
Ejemplo n.º 2
0
 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;
 }
Ejemplo n.º 3
0
        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();
                }
            }
        }