Ejemplo n.º 1
0
        public TValue this[TKey key]
        {
            get
            {
                TValue value;
                var    cacheKey = new KeyIdentifier <TKey>(key);

                if (!_cache.TryGetValue(cacheKey, out value))
                {
                    throw new KeyNotFoundException("Key:" + key + " could not be found in the cache.");
                }
                return(value);
            }

            set
            {
                if (Contains(key))
                {
                    var cacheKey = new KeyIdentifier <TKey>(key);
                    _cache[cacheKey] = value;
                }

                TryAdd(key, value);
                PerformEviction();
            }
        }
Ejemplo n.º 2
0
 public bool Contains(TKey key)
 {
     lock (this)
     {
         var cacheKey = new KeyIdentifier <TKey>(key);
         return(_cache.ContainsKey(cacheKey));
     }
 }
Ejemplo n.º 3
0
 public bool TryRemove(TKey key, out TValue value)
 {
     lock (this)
     {
         var cacheKey = new KeyIdentifier <TKey>(key);
         return(_cache.TryRemove(cacheKey, out value));
     }
 }
Ejemplo n.º 4
0
        public bool TryAdd(TKey key, TValue value)
        {
            lock (this)
            {
                var cacheKey = new KeyIdentifier <TKey>(key);

                if (!_cache.TryAdd(cacheKey, value))
                {
                    return(false);
                }

                PerformEviction();
                return(true);
            }
        }
Ejemplo n.º 5
0
        public int CompareTo(object obj)
        {
            int result = 0;

            if (obj is KeyIdentifier <TKey> )
            {
                KeyIdentifier <TKey> other = (KeyIdentifier <TKey>)obj;

                if (other._refCount > _refCount)
                {
                    result = -1;
                }
                else if (other._refCount < _refCount)
                {
                    result = 1;
                }
            }
            return(result);
        }