Show / Hide Table of Contents

Class RandomAccessCache<TKey, TValue, TWeight>

Represents concurrent cache optimized for random access. The number of items in the cache is limited by their weight.

Inheritance
object
Disposable
RandomAccessCache<TKey, TValue>
RandomAccessCache<TKey, TValue, TWeight>
Implements
IDisposable
IAsyncDisposable
IEnumerable<KeyValuePair<TKey, TValue>>
IEnumerable
Inherited Members
RandomAccessCache<TKey, TValue>.Eviction
RandomAccessCache<TKey, TValue>.KeyComparer
RandomAccessCache<TKey, TValue>.Capacity
RandomAccessCache<TKey, TValue>.ChangeAsync(TKey, CancellationToken)
RandomAccessCache<TKey, TValue>.Change(TKey, TimeSpan, CancellationToken)
RandomAccessCache<TKey, TValue>.ReplaceAsync(TKey, CancellationToken)
RandomAccessCache<TKey, TValue>.Replace(TKey, TimeSpan, CancellationToken)
RandomAccessCache<TKey, TValue>.TryRead(TKey, out RandomAccessCache<TKey, TValue>.ReadSession)
RandomAccessCache<TKey, TValue>.Contains(TKey)
RandomAccessCache<TKey, TValue>.TryRemoveAsync(TKey, CancellationToken)
RandomAccessCache<TKey, TValue>.TryRemove(TKey, out RandomAccessCache<TKey, TValue>.ReadSession, TimeSpan, CancellationToken)
RandomAccessCache<TKey, TValue>.InvalidateAsync(TKey, CancellationToken)
RandomAccessCache<TKey, TValue>.Invalidate(TKey, TimeSpan, CancellationToken)
RandomAccessCache<TKey, TValue>.InvalidateAsync(CancellationToken)
RandomAccessCache<TKey, TValue>.DisposeAsync()
RandomAccessCache<TKey, TValue>.DisposeAsyncCore()
RandomAccessCache<TKey, TValue>.Dispose(bool)
RandomAccessCache<TKey, TValue>.GetEnumerator()
Disposable.IsDisposed
Disposable.IsDisposing
Disposable.IsDisposingOrDisposed
Disposable.CreateException()
Disposable.DisposedTask
Disposable.GetDisposedTask<T>()
Disposable.TrySetDisposedException<T>(TaskCompletionSource<T>)
Disposable.TrySetDisposedException(TaskCompletionSource)
Disposable.Dispose(bool)
Disposable.DisposeAsyncCore()
Disposable.DisposeAsync()
Disposable.TryBeginDispose()
Disposable.Dispose()
Disposable.Dispose(IEnumerable<IDisposable>)
Disposable.DisposeAsync(IEnumerable<IAsyncDisposable>)
Disposable.Dispose<T>(ReadOnlySpan<T>)
Disposable.DisposeAsync(params IAsyncDisposable[])
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: DotNext.Runtime.Caching
Assembly: DotNext.Threading.dll
Syntax
public abstract class RandomAccessCache<TKey, TValue, TWeight> : RandomAccessCache<TKey, TValue>, IDisposable, IAsyncDisposable, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable where TKey : notnull where TValue : notnull where TWeight : notnull
Type Parameters
Name Description
TKey

The type of the keys.

TValue

The type of the values.

TWeight

The weight of the cache items.

Remarks

In contrast to RandomAccessCache<TKey, TValue>, the size of weight-based cache can grow to preserve collisionThreshold. When the number of collisions reaches the threshold, the cache resizes its internal structures and rehashes all the items. This process requires global lock of all items in the cache. Thus, throughput degrades. To minimize a chance of resize, you can specify initialCapacity large enough by the cost of memory footprint or increase collisionThreshold by the cost of the contention. However, TryRead(TKey, out ReadSession) never causes contention even if the cache is resizing.

Constructors

| Edit this page View Source

RandomAccessCache(int, TWeight, int)

Represents concurrent cache optimized for random access. The number of items in the cache is limited by their weight.

Declaration
protected RandomAccessCache(int initialCapacity, TWeight initialWeight, int collisionThreshold)
Parameters
Type Name Description
int initialCapacity

The expected max number of items within the cache.

TWeight initialWeight

The initial weight value.

int collisionThreshold

The maximum number of allowed hash collisions. Small number increases the memory footprint, because the cache minimizes the contention for each key. Large number decreases the memory footprint by the increased chance of the lock contention. If MaxValue is specified, then the cache doesn't grow.

Remarks

In contrast to RandomAccessCache<TKey, TValue>, the size of weight-based cache can grow to preserve collisionThreshold. When the number of collisions reaches the threshold, the cache resizes its internal structures and rehashes all the items. This process requires global lock of all items in the cache. Thus, throughput degrades. To minimize a chance of resize, you can specify initialCapacity large enough by the cost of memory footprint or increase collisionThreshold by the cost of the contention. However, TryRead(TKey, out ReadSession) never causes contention even if the cache is resizing.

Methods

| Edit this page View Source

AddWeight(ref TWeight, TKey, TValue)

Adds a weight of the specified key/value pair to the total weight.

Declaration
protected abstract void AddWeight(ref TWeight total, TKey key, TValue value)
Parameters
Type Name Description
TWeight total

The total weight of all items in the cache.

TKey key

The key of the cache item.

TValue value

The value of the cache item.

Remarks

This method can be called concurrently with RemoveWeight(ref TWeight, TKey, TValue).

| Edit this page View Source

IsEvictionRequired(ref readonly TWeight, TKey, TValue)

Checks whether the current cache has enough capacity to promote the specified key/value pair.

Declaration
protected abstract bool IsEvictionRequired(ref readonly TWeight total, TKey key, TValue value)
Parameters
Type Name Description
TWeight total

The total weight of all items in the cache.

TKey key

The key of the cache item.

TValue value

The value of the cache item.

Returns
Type Description
bool

true if the cache must evict one or more items to place a new one; otherwise, false.

| Edit this page View Source

RemoveWeight(ref TWeight, TKey, TValue)

Removes the weight of the specified cache item from the total weight.

Declaration
protected abstract void RemoveWeight(ref TWeight total, TKey key, TValue value)
Parameters
Type Name Description
TWeight total

The total weight of all items in the cache.

TKey key

The key of the cache item.

TValue value

The value of the cache item.

Remarks

This method can be called concurrently with AddWeight(ref TWeight, TKey, TValue).

Implements

IDisposable
IAsyncDisposable
IEnumerable<T>
IEnumerable

Extension Methods

BasicExtensions.As<T>(T)
BasicExtensions.GetUserData<T>(T)
BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, ReadOnlySpan<T>)
ExpressionBuilder.Const<T>(T)
AsyncLockAcquisition.AcquireLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireLockAsync<T>(T, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, bool, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, bool, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, TimeSpan, CancellationToken)
LockAcquisition.AcquireReadLock<T>(T)
LockAcquisition.AcquireReadLock<T>(T, TimeSpan)
LockAcquisition.AcquireUpgradeableReadLock<T>(T)
LockAcquisition.AcquireUpgradeableReadLock<T>(T, TimeSpan)
LockAcquisition.AcquireWriteLock<T>(T)
LockAcquisition.AcquireWriteLock<T>(T, TimeSpan)
Collection.Append<T>(IEnumerable<T>, params T[])
Collection.Copy<T>(IEnumerable<T>, int, MemoryAllocator<T>?)
Collection.ElementAt<T>(IEnumerable<T>, int, out T)
Collection.FirstOrNone<T>(IEnumerable<T>)
Collection.ForEachAsync<T>(IEnumerable<T>, Func<T, CancellationToken, ValueTask>, CancellationToken)
Collection.ForEach<T>(IEnumerable<T>, Action<T>)
Collection.LastOrNone<T>(IEnumerable<T>)
Collection.Prepend<T>(IEnumerable<T>, params T[])
Collection.SequenceHashCode<T>(IEnumerable<T>, bool)
Collection.ToAsyncEnumerable<T>(IEnumerable<T>)
Collection.ToString<T>(IEnumerable<T>, string, string)
Enumerator.GetAsyncEnumerator<T>(IEnumerable<T>, CancellationToken)
  • Edit this page
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾