ICache<TKey, TElement>

Namespace: SnowBank.Collections.Caching · interface

Implements: ICollection<KeyValuePair<TKey, TElement>>, IEnumerable<KeyValuePair<TKey, TElement>>, IEnumerable

Properties

Capacity

int Capacity { get; }

Current capacity of the cache (in number of items)

Caches that have no storage capacity limit must return int.MaxValue.

IsCapped

bool IsCapped { get; }

Indicates whether the cache has a maximum capacity (true), or has no particular limit (false)

If IsCapped returns true, consult the value of to find the maximum capacity.

KeyComparer

IEqualityComparer<TKey> KeyComparer { get; }

Comparer used for the cache keys

Methods

Cleanup

int Cleanup(Func<TKey, TElement, bool> predicate)

Finds and removes entries from the cache

  • predicate — Predicate that returns true for the entries to remove

Returns: Number of entries removed from the cache

GetOrAdd

TElement GetOrAdd(TKey key, TElement addValue)

Returns the value of an entry in the cache, creating it if necessary

  • key — Key of the entry being looked up
  • addValue — Value that will be added to the cache for this key, if it did not already exist

Returns: Value of the entry if it existed, or addValue if it did not exist

TElement GetOrAdd(TKey key, Func<TKey, TElement> factory)

Returns the value of an entry in the cache, creating it if necessary

  • key — Key of the entry being looked up
  • factory — Lambda that will be called to generate the value to add, if it did not already exist

Returns: Value of the entry if it existed, or the result of factory if it did not exist

Warning: some caches offer no guarantee that valueFactory will not be called several times!

TElement GetOrAdd<TState>(TKey key, Func<TKey, TState, TElement> factory, TState state)

Returns the value of an entry in the cache, creating it if necessary

  • key — Key of the entry being looked up
  • factory — Lambda that will be called to generate the value to add, if it did not already exist
  • state — Value passed as the second parameter to factory

Returns: Value of the entry if it existed, or the result of factory if it did not exist

Warning: some caches offer no guarantee that valueFactory will not be called several times!

Remove

bool Remove(TKey key)

Removes an entry from the cache

  • key — Key of the entry to remove

Returns: True if the entry was removed, false if it did not exist

SetItem

void SetItem(TKey key, TElement newValue)

Overwrites the value of a cache entry, creating it if necessary

  • key — Key of the entry
  • newValue — New value

TryGetValue

bool TryGetValue(TKey key, out TElement value)

Returns an entry from the cache, if it exists

  • key — Key of the entry being looked up
  • value — Receives the cached value if it exists (and is still valid)

Returns: True if the value exists in the cache; false if it does not exist (or is no longer valid)

TryRemove

bool TryRemove(TKey key, TElement expectedValue, IEqualityComparer<TElement> valueComparer = null)

Removes an entry from the cache, only if it has a specific value

  • key — Key of the entry to remove
  • expectedValue — Value that the entry must have to be removed
  • valueComparer — Optional comparer for the values

Returns: True if the entry existed and had the expected value, or false otherwise.