DotNext.Threading by .NET Foundation and Contributors

<PackageReference Include="DotNext.Threading" Version="5.17.2" />

 RandomAccessCache<TKey, TValue>

public class RandomAccessCache<TKey, TValue> : Disposable, IAsyncDisposable
Represents concurrent cache optimized for random access.
public struct ReadOrWriteSession<TKey, TValue> : IDisposable

Represents a session that can be used to read, modify or promote the cache record value.

public struct ReadSession<TKey, TValue> : IDisposable

Represents a session that can be used to read the cache record value.

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

Gets or sets a callback that can be used to clean up the evicted value.

public IEqualityComparer<TKey> KeyComparer { get; set; }

Gets or sets key comparer.

public RandomAccessCache(int cacheSize)

Initializes a new cache.

public ReadOrWriteSession<TKey, TValue> Change(TKey key, TimeSpan timeout)

Opens a session synchronously that can be used to modify the value associated with the key.

public ValueTask<ReadOrWriteSession<TKey, TValue>> ChangeAsync(TKey key, CancellationToken token = default)

Opens a session that can be used to modify the value associated with the key.

public bool Invalidate(TKey key, TimeSpan timeout)

Invalidates the cache record associated with the specified key.

public ValueTask<bool> InvalidateAsync(TKey key, CancellationToken token = default)

Invalidates the cache record associated with the specified key.

Invalidates the entire cache.

public bool TryRead(TKey key, out ReadSession session)

public bool TryRemove(TKey key, TimeSpan timeout, out ReadSession session)

public ValueTask<ReadSession<TKey, TValue>?> TryRemoveAsync(TKey key, CancellationToken token = default)

Tries to invalidate cache record associated with the provided key.