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
TKey
Entry key (identifier) type.
TValue
Entry 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
keyComparer
IEqualityComparer<TKey>Custom key equality comparer.
capacity
intAn optional maximum capacity. Equal to MaxValue by default.
removeCallback
Action<CachedItemRemovalEvent<TKey, TValue>>An optional callback which gets invoked every time an entry is removed from this cache.
Exceptions
- ArgumentOutOfRangeException
When
capacity
is 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
capacity
intAn optional maximum capacity. Equal to MaxValue by default.
removeCallback
Action<CachedItemRemovalEvent<TKey, TValue>>An optional callback which gets invoked every time an entry is removed from this cache.
Exceptions
- ArgumentOutOfRangeException
When
capacity
is 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
key
TKeyThe 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
key
does 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
key
TKeyEntry's key.
value
TValueEntry's value.
Returns
- AddOrUpdateResult
Added when new entry has been added (provided
key
did 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
key
TKeyThe key to locate.
Returns
- bool
true if the read-only dictionary contains an element that has the specified key; otherwise, false.
Exceptions
- ArgumentNullException
key
is 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
key
TKeyKey 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
key
TKeyKey of an entry to remove.
removed
TValueAn 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
key
TKeyKey of an entry to restart.
Returns
- bool
true when entry has been restarted (provided
key
exists), 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
key
TKeyEntry's key.
value
TValueEntry's value.
Returns
- bool
true when entry has been added (provided
key
did 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
key
TKeyThe key to locate.
value
TValueWhen this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the
value
parameter. 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
key
is null.