Class RandomAccessCache<TKey, TValue, TWeight>
Represents concurrent cache optimized for random access. The number of items in the cache is limited by their weight.
Inherited Members
Namespace: DotNext.Runtime.Caching
Assembly: DotNext.Threading.dll
Syntax
public abstract class RandomAccessCache<TKey, TValue, TWeight> : RandomAccessCache<TKey, TValue>, IDisposable, IAsyncDisposable 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 SourceRandomAccessCache(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 SourceAddWeight(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).
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. |
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).