Class IndividualLifetimeCache<TKey, TValue>
Represents a generic cache of keyed entries with a limited lifetime, where each entry can have its own individual lifetime.
public sealed class IndividualLifetimeCache<TKey, TValue> : IIndividualLifetimeCache<TKey, TValue>, ILifetimeCache<TKey, TValue>, IReadOnlyLifetimeCache<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
-
IndividualLifetimeCache<TKey, TValue>
- Implements
-
IIndividualLifetimeCache<TKey, TValue>ILifetimeCache<TKey, TValue>IReadOnlyLifetimeCache<TKey, TValue>ICache<TKey, TValue>IReadOnlyCache<TKey, TValue>IReadOnlyDictionary<TKey, TValue>IReadOnlyCollection<KeyValuePair<TKey, TValue>>IEnumerable<KeyValuePair<TKey, TValue>>
- Inherited Members
- Extension Methods
Remarks
Reading entries resets their lifetime.
Constructors
IndividualLifetimeCache(Timestamp, Duration, int, Action<CachedItemRemovalEvent<TKey, TValue>>?)
Creates a new IndividualLifetimeCache<TKey, TValue> instance that uses the Default key comparer.
public IndividualLifetimeCache(Timestamp startTimestamp, Duration lifetime, int capacity = 2147483647, Action<CachedItemRemovalEvent<TKey, TValue>>? removeCallback = null)
Parameters
startTimestamp
TimestampTimestamp of the creation of this cache.
lifetime
DurationLifetime of added entries.
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 or whenlifetime
is less than 1 tick.
IndividualLifetimeCache(IEqualityComparer<TKey>, Timestamp, Duration, int, Action<CachedItemRemovalEvent<TKey, TValue>>?)
Creates a new IndividualLifetimeCache<TKey, TValue> instance.
public IndividualLifetimeCache(IEqualityComparer<TKey> keyComparer, Timestamp startTimestamp, Duration lifetime, int capacity = 2147483647, Action<CachedItemRemovalEvent<TKey, TValue>>? removeCallback = null)
Parameters
keyComparer
IEqualityComparer<TKey>Custom key equality comparer.
startTimestamp
TimestampTimestamp of the creation of this cache.
lifetime
DurationLifetime of added entries.
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 or whenlifetime
is less than 1 tick.
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.
CurrentTimestamp
Timestamp at which this cache currently is.
public Timestamp CurrentTimestamp { get; }
Property Value
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.
Lifetime
Lifetime of added entries.
public Duration Lifetime { get; }
Property Value
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>>
StartTimestamp
Timestamp of the creation of this cache.
public Timestamp StartTimestamp { get; }
Property Value
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.
AddOrUpdate(TKey, TValue, Duration)
Adds a new entry or updates an existing one if key
already exists.
public AddOrUpdateResult AddOrUpdate(TKey key, TValue value, Duration lifetime)
Parameters
key
TKeyEntry's key.
value
TValueEntry's value.
lifetime
DurationEntry's lifetime.
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()
Returns an enumerator that iterates through the collection.
[Pure]
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
Returns
- IEnumerator<KeyValuePair<TKey, TValue>>
An enumerator that can be used to iterate through the collection.
GetRemainingLifetime(TKey)
Gets the remaining lifetime of an entry with the specified key
.
[Pure]
public Duration GetRemainingLifetime(TKey key)
Parameters
key
TKeyKey to check.
Returns
Move(Duration)
Moves this cache forward in time and removes entries with elapsed lifetimes.
public void Move(Duration delta)
Parameters
delta
DurationDuration to add to the CurrentTimestamp.
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.
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.
TryAdd(TKey, TValue, Duration)
Attempts to add a new entry.
public bool TryAdd(TKey key, TValue value, Duration lifetime)
Parameters
key
TKeyEntry's key.
value
TValueEntry's value.
lifetime
DurationEntry's lifetime.
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.
Exceptions
- ArgumentNullException
key
is null.