public void Add(TKey key, TValue value)
        {
            MruCache <TKey, TValue> .CacheEntry cacheEntry = new MruCache <TKey, TValue> .CacheEntry();

            DiagnosticUtility.DebugAssert(null != key, "");
            bool flag = false;

            try
            {
                if (this.items.Count == this.highWatermark)
                {
                    int num = this.highWatermark - this.lowWatermark;
                    for (int i = 0; i < num; i++)
                    {
                        TKey tKey = this.mruList.Last.Value;
                        this.mruList.RemoveLast();
                        TValue item = this.items[tKey].@value;
                        this.items.Remove(tKey);
                        this.OnSingleItemRemoved(item);
                    }
                }
                cacheEntry.node   = this.mruList.AddFirst(key);
                cacheEntry.@value = value;
                this.items.Add(key, cacheEntry);
                this.mruEntry = cacheEntry;
                flag          = true;
            }
            finally
            {
                if (!flag)
                {
                    this.Clear();
                }
            }
        }
        public bool TryGetValue(TKey key, out TValue value)
        {
            MruCache <TKey, TValue> .CacheEntry cacheEntry;
            if (this.mruEntry.node != null && key != null && key.Equals(this.mruEntry.node.Value))
            {
                value = this.mruEntry.@value;
                return(true);
            }
            bool flag = this.items.TryGetValue(key, out cacheEntry);

            value = cacheEntry.@value;
            if (flag && this.mruList.Count > 1 && !object.ReferenceEquals(this.mruList.First, cacheEntry.node))
            {
                this.mruList.Remove(cacheEntry.node);
                this.mruList.AddFirst(cacheEntry.node);
                this.mruEntry = cacheEntry;
            }
            return(flag);
        }
예제 #3
0
 static DnsCache()
 {
     DnsCache.ResolveCache = new MruCache <string, DnsCache.DnsCacheEntry>(64);
     DnsCache.CacheTimeout = TimeSpan.FromSeconds(2);
 }