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)); } } }
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); }