示例#1
0
 private void TryAddToCachedItems(TKey key, RecyclingBoundedCacheState currentState, TValue value)
 {
     if (currentState.Items.TryAdd(key, value))
     {
         var newCount = Interlocked.Increment(ref currentState.Count);
         if (newCount > capacity)
         {
             Interlocked.Exchange(ref state, new RecyclingBoundedCacheState(comparer));
         }
     }
 }
示例#2
0
        public RecyclingBoundedCache(int capacity, [CanBeNull] IEqualityComparer <TKey> comparer = null)
        {
            if (capacity < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(capacity), "The capacity must be non-negative");
            }

            this.capacity = capacity;
            this.comparer = comparer ?? EqualityComparer <TKey> .Default;

            state = new RecyclingBoundedCacheState(this.comparer);
        }