Exemplo n.º 1
0
 public void AddOrUpdate(TKey key, TValue value)
 {
     lock (cacheLock)
     {
         if (cache.TryGetValue(key, out LruListItem <TKey, TValue> value2))
         {
             value2.Value = value;
             lruList.Touch(value2);
         }
         else
         {
             LruListItem <TKey, TValue> lruListItem = new LruListItem <TKey, TValue>(key, value);
             while (cache.Count >= MaxEntries)
             {
                 cache.Remove(lruList.EvictOldest());
             }
             lruList.Add(lruListItem);
             cache.Add(key, lruListItem);
         }
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Add the key/value pair to the cache, or update
 /// the value if the key already exists.
 ///
 /// If the cache is full, evicts the least recently used item.
 /// </summary>
 /// <param name="key"></param>
 /// <param name="value"></param>
 public void AddOrUpdate(TKey key, TValue value)
 {
     lock (cacheLock)
     {
         LruListItem <TKey, TValue> existingLruListItem;
         if (cache.TryGetValue(key, out existingLruListItem))
         {
             // update
             existingLruListItem.Value = value;
             lruList.Touch(existingLruListItem);
         }
         else
         {
             // add
             var newLruListItem = new LruListItem <TKey, TValue>(key, value);
             while (cache.Count >= MaxEntries)
             {
                 cache.Remove(lruList.EvictOldest());
             }
             lruList.Add(newLruListItem);
             cache.Add(key, newLruListItem);
         }
     }
 }