DotNext by .NET Foundation and Contributors

<PackageReference Include="DotNext" Version="5.17.0" />

 ConcurrentCache<TKey, TValue>

public class ConcurrentCache<TKey, TValue> : IReadOnlyDictionary<TKey, TValue>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable, IReadOnlyCollection<KeyValuePair<TKey, TValue>>
Represents concurrent cache.
public int Capacity { get; }

Gets the capacity of this cache.

public int Count { get; }

Gets the number of cache entries.

public Action<TKey, TValue> Eviction { get; set; }

Gets or sets a handler that can be used to capture evicted cache items.

public bool ExecuteEvictionAsynchronously { get; set; }

Gets or sets a value indicating that Eviction callback must be executed asynchronously.

public TValue this[TKey key] { get; set; }

Gets or sets cache entry.

public ConcurrentCache(int capacity, int concurrencyLevel, CacheEvictionPolicy evictionPolicy, IEqualityComparer<TKey> keyComparer = null)

Initializes a new empty cache.

public ConcurrentCache(int capacity, CacheEvictionPolicy evictionPolicy, IEqualityComparer<TKey> keyComparer = null)

Initializes a new empty cache.

public TValue AddOrUpdate(TKey key, TValue value, out bool added)

Adds or modifies the cache entry as an atomic operation.

public void Clear()

Removes all items from the cache.

public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()

Gets enumerator over all key/value pairs.

public TValue GetOrAdd(TKey key, TValue value, out bool added)

Gets or adds the cache entry as an atomic operation.

public int TakeSnapshot(Span<KeyValuePair<TKey, TValue>> buffer, bool descendingOrder = true)

Gets a sorted set of cache entries.

public bool TryAdd(TKey key, TValue value)

Adds a new cache entry if the cache is not full.

public bool TryGetValue(TKey key, out TValue value)

Attempts to get existing cache entry.

public bool TryRemove(TKey key, out TValue value)

Attempts to remove the cache entry.

public bool TryRemove(KeyValuePair<TKey, TValue> pair)

Attempts to remove the cache entry.

public bool TryUpdate(TKey key, TValue newValue, TValue expectedValue)

Updates the cache entry associated with the specified key.