Class Cache<TKey, TValue>
Represents a generic cache of keyed entries.
public sealed class Cache<TKey, TValue> : ICache<TKey, TValue>, IReadOnlyCache<TKey, TValue>, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable where TKey : notnull
Type Parameters
TKeyEntry key (identifier) type.
TValueEntry value type.
- Inheritance
-
Cache<TKey, TValue>
- Implements
-
ICache<TKey, TValue>IReadOnlyCache<TKey, TValue>IReadOnlyDictionary<TKey, TValue>IReadOnlyCollection<KeyValuePair<TKey, TValue>>IEnumerable<KeyValuePair<TKey, TValue>>
- Inherited Members
- Extension Methods
Remarks
New entries added to this cache are added as Newest.
Constructors
Cache(IEqualityComparer<TKey>, int, Action<CachedItemRemovalEvent<TKey, TValue>>?)
Creates a new empty Cache<TKey, TValue> instance that uses a custom key comparer.
public Cache(IEqualityComparer<TKey> keyComparer, int capacity = 2147483647, Action<CachedItemRemovalEvent<TKey, TValue>>? removeCallback = null)
Parameters
keyComparerIEqualityComparer<TKey>Custom key equality comparer.
capacityintAn optional maximum capacity. Equal to MaxValue by default.
removeCallbackAction<CachedItemRemovalEvent<TKey, TValue>>An optional callback which gets invoked every time an entry is removed from this cache.
Exceptions
- ArgumentOutOfRangeException
When
capacityis less than 1.
Cache(int, Action<CachedItemRemovalEvent<TKey, TValue>>?)
Creates a new empty Cache<TKey, TValue> instance that uses the Default key comparer.
public Cache(int capacity = 2147483647, Action<CachedItemRemovalEvent<TKey, TValue>>? removeCallback = null)
Parameters
capacityintAn optional maximum capacity. Equal to MaxValue by default.
removeCallbackAction<CachedItemRemovalEvent<TKey, TValue>>An optional callback which gets invoked every time an entry is removed from this cache.
Exceptions
- ArgumentOutOfRangeException
When
capacityis less than 1.
Properties
Capacity
Maximum capacity of this cache. Adding entries to caches whose capacity have been reached will cause their Oldest entry to be removed.
public int Capacity { get; }
Property Value
Comparer
IEqualityComparer<T> instance used for key equality comparison.
public IEqualityComparer<TKey> Comparer { get; }
Property Value
- IEqualityComparer<TKey>
Count
Gets the number of elements in the collection.
public int Count { get; }
Property Value
- int
The number of elements in the collection.
this[TKey]
Gets or sets the entry that has the specified key.
public TValue this[TKey key] { get; set; }
Parameters
keyTKeyThe key of the entry to get or set.
Property Value
- TValue
Remarks
See AddOrUpdate(TKey, TValue) for more information about the setter's behavior.
Exceptions
- KeyNotFoundException
Entry with the provided
keydoes not exist.
Keys
Gets an enumerable collection that contains the keys in the read-only dictionary.
public IEnumerable<TKey> Keys { get; }
Property Value
- IEnumerable<TKey>
An enumerable collection that contains the keys in the read-only dictionary.
Newest
Currently newest cache entry.
public KeyValuePair<TKey, TValue>? Newest { get; }
Property Value
- KeyValuePair<TKey, TValue>?
Oldest
Currently oldest cache entry.
public KeyValuePair<TKey, TValue>? Oldest { get; }
Property Value
- KeyValuePair<TKey, TValue>?
Remarks
RemoveCallback
An optional callback which gets invoked every time an entry is removed from this cache.
public Action<CachedItemRemovalEvent<TKey, TValue>>? RemoveCallback { get; }
Property Value
- Action<CachedItemRemovalEvent<TKey, TValue>>
Values
Gets an enumerable collection that contains the values in the read-only dictionary.
public IEnumerable<TValue> Values { get; }
Property Value
- IEnumerable<TValue>
An enumerable collection that contains the values in the read-only dictionary.
Methods
AddOrUpdate(TKey, TValue)
Adds a new entry or updates an existing one if key already exists.
public AddOrUpdateResult AddOrUpdate(TKey key, TValue value)
Parameters
keyTKeyEntry's key.
valueTValueEntry's value.
Returns
- AddOrUpdateResult
Added when new entry has been added (provided
keydid not exist), otherwise Updated.
Remarks
When new entry addition causes Count to exceed Capacity, then the Oldest entry will be removed automatically.
Clear()
Removes all entries from the collection.
public void Clear()
ContainsKey(TKey)
Determines whether the read-only dictionary contains an element that has the specified key.
[Pure]
public bool ContainsKey(TKey key)
Parameters
keyTKeyThe key to locate.
Returns
- bool
true if the read-only dictionary contains an element that has the specified key; otherwise, false.
Exceptions
- ArgumentNullException
keyis null.
GetEnumerator()
Creates a new LinkedListSlimNodeEnumerator<T> instance for this cache.
[Pure]
public LinkedListSlimNodeEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
Returns
- LinkedListSlimNodeEnumerator<KeyValuePair<TKey, TValue>>
New LinkedListSlimNodeEnumerator<T> instance.
Remove(TKey)
Attempts to remove an entry with the specified key.
public bool Remove(TKey key)
Parameters
keyTKeyKey of an entry to remove.
Returns
- bool
true when entry has been removed, otherwise false.
Remove(TKey, out TValue)
Attempts to remove an entry with the specified key and to return a value associated with that key.
public bool Remove(TKey key, out TValue removed)
Parameters
keyTKeyKey of an entry to remove.
removedTValueAn out parameter that returns a value associated with the
key, if it exists.
Returns
- bool
true when entry has been removed, otherwise false.
Restart(TKey)
Attempts to restart an entry associated with the specified key by e.g. moving it to the top of the cache.
public bool Restart(TKey key)
Parameters
keyTKeyKey of an entry to restart.
Returns
- bool
true when entry has been restarted (provided
keyexists), otherwise false.
Remarks
Marks an entry associated with the specified key as Newest, if it exists.
TryAdd(TKey, TValue)
Attempts to add a new entry.
public bool TryAdd(TKey key, TValue value)
Parameters
keyTKeyEntry's key.
valueTValueEntry's value.
Returns
- bool
true when entry has been added (provided
keydid not exist), otherwise false.
Remarks
When new entry addition causes Count to exceed Capacity, then the Oldest entry will be removed automatically.
TryGetValue(TKey, out TValue)
Gets the value that is associated with the specified key.
public bool TryGetValue(TKey key, out TValue value)
Parameters
keyTKeyThe key to locate.
valueTValueWhen this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the
valueparameter. This parameter is passed uninitialized.
Returns
- bool
true if the object that implements the IReadOnlyDictionary<TKey, TValue> interface contains an element that has the specified key; otherwise, false.
Remarks
Restarts an entry associated with the specified key, if it exists.
See Restart(TKey) for more information.
Exceptions
- ArgumentNullException
keyis null.