/// <summary> /// The on insert. /// </summary> /// <param name="key"> /// The key. /// </param> /// <param name="value"> /// The value. /// </param> protected override void OnInsert([NotNull] object key, [NotNull] object value) { CodeContracts.ArgumentNotNull(key, "key"); CodeContracts.ArgumentNotNull(value, "value"); if (this.Dictionary.Keys.Count >= this.m_max) { // Purge an item from the cache DoubleLinkedList.LinkItem tail = this.m_list.TailLink; if (tail != null) { object purgeKey = this.m_linkToKey[tail]; if (purgeKey != null) { // Fire the event if (this.OnPurgedFromCache != null && this.OnPurgedFromCache.GetInvocationList().Length > 0) { this.OnPurgedFromCache(purgeKey, tail.Item); } this.Remove(purgeKey); } } } }
/// <summary> /// The add. /// </summary> /// <param name="key"> /// The key. /// </param> /// <param name="value"> /// The value. /// </param> public void Add([NotNull] object key, [NotNull] object value) { CodeContracts.ArgumentNotNull(key, "key"); CodeContracts.ArgumentNotNull(value, "value"); DoubleLinkedList.LinkItem link = this.m_list.Prepend(value); this.Dictionary.Add(key, link); // Keep a reverse index from the link to the key this.m_linkToKey[link] = key; }
/// <summary> /// The this. /// </summary> /// <param name="key"> /// The key. /// </param> public object this[[NotNull] object key] { get { CodeContracts.ArgumentNotNull(key, "key"); var item = (DoubleLinkedList.LinkItem) this.Dictionary[key]; if (item == null) { return(null); } this.m_list.MoveToHead(item); return(item.Item); } set { CodeContracts.ArgumentNotNull(key, "key"); CodeContracts.ArgumentNotNull(value, "value"); DoubleLinkedList.LinkItem link = null; if (this.Dictionary.Contains(key)) { link = (DoubleLinkedList.LinkItem) this.Dictionary[key]; link.Item = value; this.m_list.MoveToHead(link); this.Dictionary[key] = link; // Keep a reverse index from the link to the key this.m_linkToKey[link] = key; } else { this.Add(key, value); } } }