QuasiImmutableCache<TKey, TValue>

Namespace: SnowBank.Collections.Caching · class

Implements: ICache<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>

Implements a cache with values that do not frequently change

Remarks

This cache is optimized for when the application only handle a small number of 'static' values, which will be quickly populated during startup, and then with very infrequent additions

The cache uses Copy-on-write semantics to publish a new updated snapshot that includes the new cached value.

The cache is guaranteed to be thread-safe. When concurrent threads attempt to insert the same new key, only one will "win" the race, and the others threads will discard their own instance and use the instance created by the winning thread.

Constructors

QuasiImmutableCache<TKey, TValue>

QuasiImmutableCache<TKey, TValue>()

QuasiImmutableCache<TKey, TValue>(Func<TKey, TValue> valueFactory)

QuasiImmutableCache<TKey, TValue>(IEqualityComparer<TKey> keyComparer)

QuasiImmutableCache<TKey, TValue>(Func<TKey, TValue> valueFactory, IEqualityComparer<TKey> keyComparer, IEqualityComparer<TValue> valueComparer = null)

QuasiImmutableCache<TKey, TValue>(FrozenDictionary<TKey, TValue> items, Func<TKey, TValue> valueFactory = null, IEqualityComparer<TValue> valueComparer = null)

Properties

Count

int Count { get; }

Gets the number of elements contained in the cache.

Factory

Func<TKey, TValue> Factory { get; }

Factory method (optional) used to construct new values in the cache, if one is not already provided.

Only used by .

Item

TValue Item { get; }

KeyComparer

IEqualityComparer<TKey> KeyComparer { get; }

Instance used to compare the keys of the cache

Keys

ImmutableArray<TKey> Keys { get; }

Gets an enumerable collection that contains the keys in the read-only dictionary.

ValueComparer

IEqualityComparer<TValue> ValueComparer { get; }

Instance used to compare the values of the cache

Values

ImmutableArray<TValue> Values { get; }

Gets an enumerable collection that contains the values in the read-only dictionary.

Methods

AddOrUpdate

bool AddOrUpdate(TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)

Adds or update an entry in the cache

  • key — Key of the entry in the cache
  • addValue — Value to add, if the key is not present
  • updateValueFactory — Method that will produce an update value, if the key is already present

Returns: true if the value was added, or false if it was updated

AddRange

void AddRange(IEnumerable<KeyValuePair<TKey, TValue>> items)

Adds multiple entries to the cache, in a single "transaction".

  • items — List of all the new key/value pairs to insert into the cache

If at least one entry already exists, an exception is thrown and no changes will be made to the cache

Clear

void Clear()

Removes all items from the cache.

ContainsKey

bool ContainsKey(TKey key)

Determines whether the read-only dictionary contains an element that has the specified key.

ContainsValue

bool ContainsValue(TValue value)

Determines whether the read-only dictionary contains an element that has the specified value.

GetEnumerator

Enumerator GetEnumerator()

Returns an enumerator that will list all the entries in the cache

Returns: The enumerator will capture a snapshot of the cache. Any changes made after this call returns will not be observed by the enumerator.

GetOrAdd

TValue GetOrAdd(TKey key)

Gets the value of a key in the cache, or add a new value using the default factory method

  • key — Key in the cache to lookup

Returns: Existing cached value, or newly created value if it was not present before

This method is only supported if Factory is not null.

If multiple threads are attempting to concurrently populate the same key in the cache, they will all observe the same instance.

TValue GetOrAdd(TKey key, TValue value)

Gets the value of an entry in the cache, or add an already constructed value it is not present in the cache

  • key — Key of the entry in the cache
  • value — Already constructed value that should be added to the cache if the key does not exist already

Returns: Value that was already present in the cache, or value if it was not present.

If multiple threads are attempting to concurrently populate the same key in the cache, they will all observe the same instance.

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

Gets the value of an entry in the cache, or add a new value created using the provided factory method

  • key — Key of the entry in the cache
  • factory — Factory method that will be called if a new value must be created.

Returns: Value that was already present in the cache, or the result of calling factory if it was not present.

If multiple threads are attempting to concurrently populate the same key in the cache, they will all observe the same instance.

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

Gets the value of an entry in the cache, or add a new value created using the provided factory method

  • key — Key of the entry in the cache
  • factory — Factory method that will be called if a new value must be created.
  • state — Opaque value that is passed to the factory method.

Returns: Value that was already present in the cache, or the result of calling factory if it was not present.

If multiple threads are attempting to concurrently populate the same key in the cache, they will all observe the same instance.

GetValueOrDefault

TValue GetValueOrDefault(TKey key, TValue defaultValue)

Returns the value of an entry in the cache, or a default value it is not in the cache.

Remove

bool Remove(TKey key)

Removes en entry from the cache

  • key — Key of the entry to remove from the cache

Returns: true if an entry with this key was found and removed; otherwise, false.

SetItem

void SetItem(TKey key, TValue value)

Sets the value of an entry in the cache, regardless of its previous state

  • key — Key of the entry to replace
  • value — New value for this entry

SetItems

void SetItems(IEnumerable<KeyValuePair<TKey, TValue>> items)

Adds or replace multiple entries in the cache, in single "transaction".

  • items — List of all key/value pairs to insert or replace into the cache

Entries that were already present in the cache will be overwritten by the new value in

ToArray

KeyValuePair<TKey, TValue>[] ToArray()

Returns an array with all the entries in the cache

ToList

List<KeyValuePair<TKey, TValue>> ToList()

Returns a list with all the entries in the cache

TryGetValue

bool TryGetValue(TKey key, out TValue value)

Returns the value of an entry in the cache, if it exists.

TryRemove

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

Removes an entry from the cache, only if it had the expected value

  • key — Key of the entry to remove
  • expectedValue — Value that the entry is expected to have
  • valueComparer — Optional value comparer

Returns: true if an entry with this key and value was found and removed; otherwise, false.