Class ConcurrentCache<TKey, TValue>
Represents concurrent cache.
Implements
Inherited Members
Namespace: DotNext .Runtime.Caching
Assembly: DotNext.dll
Syntax
[Obsolete("Use RandomAccessCache from DotNext.Threading library instead.")]
public class ConcurrentCache<TKey, TValue> : IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable where TKey : notnull
Type Parameters
Name | Description |
---|---|
TKey | The type of the keys. |
TValue | The type of the cache items. |
Remarks
The cache provides O(1) lookup performance if there is no hash collision. Asymptotic complexity of other
operations are same as for Concurrent
Constructors
| Edit this page View SourceConcurrentCache(int, CacheEvictionPolicy, IEqualityComparer<TKey>?)
Initializes a new empty cache.
Declaration
public ConcurrentCache(int capacity, CacheEvictionPolicy evictionPolicy, IEqualityComparer<TKey>? keyComparer = null)
Parameters
Type | Name | Description |
---|---|---|
int | capacity | The maximum number of cached items. |
Cache |
evictionPolicy | The eviction policy describing how the cached items must be removed on cache overflow. |
IEquality |
keyComparer | The comparer that can be used to compare keys within the cache. |
Exceptions
Type | Condition |
---|---|
Argument |
|
ConcurrentCache(int, int, CacheEvictionPolicy, IEqualityComparer<TKey>?)
Initializes a new empty cache.
Declaration
public ConcurrentCache(int capacity, int concurrencyLevel, CacheEvictionPolicy evictionPolicy, IEqualityComparer<TKey>? keyComparer = null)
Parameters
Type | Name | Description |
---|---|---|
int | capacity | The maximum number of cached items. |
int | concurrencyLevel | The number of thread that can access the cache concurrently. |
Cache |
evictionPolicy | The eviction policy describing how the cached items must be removed on cache overflow. |
IEquality |
keyComparer | The comparer that can be used to compare keys within the cache. |
Exceptions
Type | Condition |
---|---|
Argument |
|
Properties
| Edit this page View SourceCapacity
Gets the capacity of this cache.
Declaration
public int Capacity { get; }
Property Value
Type | Description |
---|---|
int |
Count
Gets the number of cache entries.
Declaration
public int Count { get; }
Property Value
Type | Description |
---|---|
int |
Remarks
In contrast to Count, this property is fast and doesn't require an exclusive lock.
Eviction
Gets or sets a handler that can be used to capture evicted cache items.
Declaration
public Action<TKey, TValue>? Eviction { get; init; }
Property Value
Type | Description |
---|---|
Action<TKey, TValue> |
ExecuteEvictionAsynchronously
Gets or sets a value indicating that Eviction callback must be executed asynchronously.
Declaration
public bool ExecuteEvictionAsynchronously { get; init; }
Property Value
Type | Description |
---|---|
bool |
this[TKey]
Gets or sets cache entry.
Declaration
public TValue this[TKey key] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key of the cache entry. |
Property Value
Type | Description |
---|---|
TValue | The cache entry. |
Exceptions
Type | Condition |
---|---|
Key |
The cache entry with |
Methods
| Edit this page View SourceAddOrUpdate(TKey, TValue, out bool)
Adds or modifies the cache entry as an atomic operation.
Declaration
public TValue AddOrUpdate(TKey key, TValue value, out bool added)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key of the cache entry. |
TValue | value | The cache entry. |
bool | added | true if a new entry is added; false if the existing entry is modified. |
Returns
Type | Description |
---|---|
TValue |
|
Clear()
Removes all items from the cache.
Declaration
public void Clear()
Remarks
This operation locks the entire cache exclusively.
GetEnumerator()
Gets enumerator over all key/value pairs.
Declaration
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
Returns
Type | Description |
---|---|
IEnumerator<Key |
Unsorted enumerator over all key/value pairs. |
GetOrAdd(TKey, TValue, out bool)
Gets or adds the cache entry as an atomic operation.
Declaration
public TValue GetOrAdd(TKey key, TValue value, out bool added)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key of the cache entry. |
TValue | value | The cache entry. |
bool | added | true if a new entry is added; false if the entry is already exist. |
Returns
Type | Description |
---|---|
TValue |
|
TakeSnapshot(Span<KeyValuePair<TKey, TValue>>, bool)
Gets a sorted set of cache entries.
Declaration
public int TakeSnapshot(Span<KeyValuePair<TKey, TValue>> buffer, bool descendingOrder = true)
Parameters
Type | Name | Description |
---|---|---|
Span<Key |
buffer | The buffer used as a destination to write cache entries. |
bool | descendingOrder | true to start from the least (or most) recently used cache entry; false to start from the eldest used cache entry. |
Returns
Type | Description |
---|---|
int | The actual number of written items. |
Remarks
In contrast to Get
TryAdd(TKey, TValue)
Adds a new cache entry if the cache is not full.
Declaration
public bool TryAdd(TKey key, TValue value)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key of the cache entry. |
TValue | value | The cache entry. |
Returns
| Edit this page View SourceTryGetValue(TKey, out TValue)
Attempts to get existing cache entry.
Declaration
public bool TryGetValue(TKey key, out TValue value)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key of the cache entry. |
TValue | value | The cache entry, if successful. |
Returns
| Edit this page View SourceTryRemove(KeyValuePair<TKey, TValue>)
Attempts to remove the cache entry.
Declaration
public bool TryRemove(KeyValuePair<TKey, TValue> pair)
Parameters
Type | Name | Description |
---|---|---|
Key |
pair | The pair to be removed. |
Returns
Remarks
This method will not raise Eviction event for the removed entry.
TryRemove(TKey, out TValue)
Attempts to remove the cache entry.
Declaration
public bool TryRemove(TKey key, out TValue value)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key of the cache entry. |
TValue | value | The cache entry, if successful. |
Returns
Remarks
This method will not raise Eviction event for the removed entry.
TryUpdate(TKey, TValue, TValue)
Updates the cache entry associated with the specified key.
Declaration
public bool TryUpdate(TKey key, TValue newValue, TValue expectedValue)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key whose value is compared with |
TValue | newValue | The value that replaces the value of the entry with |
TValue | expectedValue | The value that is compared to the value of the element with |
Returns
Type | Description |
---|---|
bool | true if the value with |