Class RandomAccessCache<TKey, TValue>
Represents concurrent cache optimized for random access.
Inherited Members
Namespace: DotNext.Runtime.Caching
Assembly: DotNext.Threading.dll
Syntax
public class RandomAccessCache<TKey, TValue> : Disposable, IDisposable, IAsyncDisposable where TKey : notnull where TValue : notnull
Type Parameters
Name | Description |
---|---|
TKey | The type of the keys. |
TValue | The type of the values. |
Remarks
The cache evicts older records on overflow.
Constructors
| Edit this page View SourceRandomAccessCache(int)
Initializes a new cache.
Declaration
public RandomAccessCache(int cacheSize)
Parameters
Type | Name | Description |
---|---|---|
int | cacheSize | Maximum cache size. |
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException |
|
Properties
| Edit this page View SourceEviction
Gets or sets a callback that can be used to clean up the evicted value.
Declaration
public Action<TKey, TValue>? Eviction { get; init; }
Property Value
Type | Description |
---|---|
Action<TKey, TValue> |
KeyComparer
Gets or sets key comparer.
Declaration
public IEqualityComparer<TKey>? KeyComparer { get; init; }
Property Value
Type | Description |
---|---|
IEqualityComparer<TKey> |
Methods
| Edit this page View SourceChange(TKey, TimeSpan)
Opens a session synchronously that can be used to modify the value associated with the key.
Declaration
public RandomAccessCache<TKey, TValue>.ReadOrWriteSession Change(TKey key, TimeSpan timeout)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key of the cache record. |
TimeSpan | timeout | The time to wait for the cache lock. |
Returns
Type | Description |
---|---|
RandomAccessCache<TKey, TValue>.ReadOrWriteSession | The session that can be used to read or modify the cache record. |
Remarks
The cache guarantees that the value cannot be evicted concurrently with the returned session. However, the value can be evicted immediately after. The caller must dispose session.
Exceptions
Type | Condition |
---|---|
TimeoutException | The internal lock cannot be acquired in timely manner. |
ObjectDisposedException | The cache is disposed. |
ChangeAsync(TKey, CancellationToken)
Opens a session that can be used to modify the value associated with the key.
Declaration
public ValueTask<RandomAccessCache<TKey, TValue>.ReadOrWriteSession> ChangeAsync(TKey key, CancellationToken token = default)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key of the cache record. |
CancellationToken | token | The token that can be used to cancel the operation. |
Returns
Type | Description |
---|---|
ValueTask<RandomAccessCache<TKey, TValue>.ReadOrWriteSession> | The session that can be used to read or modify the cache record. |
Remarks
The cache guarantees that the value cannot be evicted concurrently with the returned session. However, the value can be evicted immediately after. The caller must dispose session.
Exceptions
Type | Condition |
---|---|
OperationCanceledException | The operation has been canceled. |
ObjectDisposedException | The cache is disposed. |
Dispose(bool)
Releases managed and unmanaged resources associated with this object.
Declaration
protected override void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
bool | disposing | true if called from Dispose(); false if called from finalizer ~Disposable(). |
Overrides
| Edit this page View SourceDisposeAsync()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
Declaration
public ValueTask DisposeAsync()
Returns
Type | Description |
---|---|
ValueTask | A task that represents the asynchronous dispose operation. |
DisposeAsyncCore()
Releases managed resources associated with this object asynchronously.
Declaration
protected override ValueTask DisposeAsyncCore()
Returns
Type | Description |
---|---|
ValueTask | The task representing asynchronous execution of this method. |
Overrides
Remarks
This method makes sense only if derived class implements IAsyncDisposable interface.
Invalidate(TKey, TimeSpan)
Invalidates the cache record associated with the specified key.
Declaration
public bool Invalidate(TKey key, TimeSpan timeout)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key of the cache record to be removed. |
TimeSpan | timeout | The time to wait for the cache lock. |
Returns
Type | Description |
---|---|
bool | true if the cache record associated with |
Exceptions
Type | Condition |
---|---|
TimeoutException | The internal lock cannot be acquired in timely manner. |
ObjectDisposedException | The cache is disposed. |
InvalidateAsync(CancellationToken)
Invalidates the entire cache.
Declaration
public ValueTask InvalidateAsync(CancellationToken token = default)
Parameters
Type | Name | Description |
---|---|---|
CancellationToken | token | The token that can be used to cancel the operation. |
Returns
Type | Description |
---|---|
ValueTask |
Exceptions
Type | Condition |
---|---|
OperationCanceledException | The operation has been canceled. |
ObjectDisposedException | The cache is disposed. |
InvalidateAsync(TKey, CancellationToken)
Invalidates the cache record associated with the specified key.
Declaration
public ValueTask<bool> InvalidateAsync(TKey key, CancellationToken token = default)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key of the cache record to be removed. |
CancellationToken | token | The token that can be used to cancel the operation. |
Returns
Type | Description |
---|---|
ValueTask<bool> | true if the cache record associated with |
Exceptions
Type | Condition |
---|---|
OperationCanceledException | The operation has been canceled. |
ObjectDisposedException | The cache is disposed. |
TryRead(TKey, out ReadSession)
Tries to read the cached record.
Declaration
public bool TryRead(TKey key, out RandomAccessCache<TKey, TValue>.ReadSession session)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key of the cache record. |
RandomAccessCache<TKey, TValue>.ReadSession | session | A session that can be used to read the cached record. |
Returns
Type | Description |
---|---|
bool | true if the record is available for reading and the session is active; otherwise, false. |
Remarks
The cache guarantees that the value cannot be evicted concurrently with the session. However, the value can be evicted immediately after. The caller must dispose session.
TryRemove(TKey, TimeSpan, out ReadSession)
Tries to invalidate cache record associated with the provided key synchronously.
Declaration
public bool TryRemove(TKey key, TimeSpan timeout, out RandomAccessCache<TKey, TValue>.ReadSession session)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key of the cache record to be removed. |
TimeSpan | timeout | The time to wait for the cache lock. |
RandomAccessCache<TKey, TValue>.ReadSession | session | The session that can be used to read the removed cache record. |
Returns
Type | Description |
---|---|
bool | true if the record associated with |
Exceptions
Type | Condition |
---|---|
TimeoutException | The internal lock cannot be acquired in timely manner. |
ObjectDisposedException | The cache is disposed. |
TryRemoveAsync(TKey, CancellationToken)
Tries to invalidate cache record associated with the provided key.
Declaration
public ValueTask<RandomAccessCache<TKey, TValue>.ReadSession?> TryRemoveAsync(TKey key, CancellationToken token = default)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key of the cache record to be removed. |
CancellationToken | token | The token that can be used to cancel the operation. |
Returns
Type | Description |
---|---|
ValueTask<RandomAccessCache<TKey, TValue>.ReadSession?> | The session that can be used to read the removed cache record;
or null if there is no record associated with |
Exceptions
Type | Condition |
---|---|
OperationCanceledException | The operation has been canceled. |
ObjectDisposedException | The cache is disposed. |