Table of Contents

Class CachedLoader<TKey, TValue>

Namespace
Utils.Collections
Assembly
Utils.dll

A class that provides a caching mechanism for loading resources. If a resource is not already loaded, it will be loaded and cached upon first access.

public class CachedLoader<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable where TKey : notnull

Type Parameters

TKey

The type of the keys used to identify resources.

TValue

The type of the resources being cached.

Inheritance
CachedLoader<TKey, TValue>
Implements
IDictionary<TKey, TValue>
ICollection<KeyValuePair<TKey, TValue>>
IEnumerable<KeyValuePair<TKey, TValue>>
Inherited Members
Extension Methods

Constructors

CachedLoader()

Initializes a new instance of the CachedLoader<TKey, TValue> class with auto-create enabled by default.

public CachedLoader()

CachedLoader(TryLoadValueDelegate)

Initializes a new instance of the CachedLoader<TKey, TValue> class with a specified loader function.

public CachedLoader(CachedLoader<TKey, TValue>.TryLoadValueDelegate loader)

Parameters

loader CachedLoader<TKey, TValue>.TryLoadValueDelegate

The function used to load resources.

CachedLoader(TryLoadValueDelegate, IDictionary<TKey, TValue>)

Initializes a new instance of the CachedLoader<TKey, TValue> class with a specified loader function and cache holder.

public CachedLoader(CachedLoader<TKey, TValue>.TryLoadValueDelegate loader, IDictionary<TKey, TValue> cacheHolder)

Parameters

loader CachedLoader<TKey, TValue>.TryLoadValueDelegate

The function used to load resources.

cacheHolder IDictionary<TKey, TValue>

The dictionary used to hold the cached resources.

Properties

AutoCreateObject

Gets or sets a value indicating whether to automatically create and cache a resource if it is not found.

public bool AutoCreateObject { get; set; }

Property Value

bool

Count

Gets the number of elements contained in the cache.

public int Count { get; }

Property Value

int

IsReadOnly

Gets a value indicating whether the cache is read-only.

public bool IsReadOnly { get; }

Property Value

bool

this[TKey]

Gets or sets the resource associated with the specified key. If the resource is not found and AutoCreateObject is true, the resource will be loaded and cached.

public TValue this[TKey key] { get; set; }

Parameters

key TKey

The key of the resource to get or set.

Property Value

TValue

The resource associated with the specified key.

Keys

Gets a collection containing the keys of the cache.

public ICollection<TKey> Keys { get; }

Property Value

ICollection<TKey>

Values

Gets a collection containing the values in the cache.

public ICollection<TValue> Values { get; }

Property Value

ICollection<TValue>

Methods

Add(KeyValuePair<TKey, TValue>)

Adds a key-value pair to the cache.

public void Add(KeyValuePair<TKey, TValue> item)

Parameters

item KeyValuePair<TKey, TValue>

The key-value pair to add.

Add(TKey, TValue)

Adds the specified key-value pair to the cache.

public void Add(TKey key, TValue value)

Parameters

key TKey

The key of the element to add.

value TValue

The value of the element to add.

Clear()

Removes all elements from the cache.

public void Clear()

Contains(KeyValuePair<TKey, TValue>)

Determines whether the cache contains a specific key-value pair.

public bool Contains(KeyValuePair<TKey, TValue> item)

Parameters

item KeyValuePair<TKey, TValue>

The key-value pair to locate in the cache.

Returns

bool

true if the cache contains the specified key-value pair; otherwise, false.

ContainsKey(TKey)

Determines whether the cache contains an element with the specified key.

public bool ContainsKey(TKey key)

Parameters

key TKey

The key to locate in the cache.

Returns

bool

true if the cache contains an element with the specified key; otherwise, false.

CopyTo(KeyValuePair<TKey, TValue>[], int)

Copies the elements of the cache to an array, starting at a particular array index.

public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)

Parameters

array KeyValuePair<TKey, TValue>[]

The one-dimensional array that is the destination of the elements copied from the cache.

arrayIndex int

The zero-based index in the array at which copying begins.

GetEnumerator()

Returns an enumerator that iterates through the cache.

public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()

Returns

IEnumerator<KeyValuePair<TKey, TValue>>

An enumerator for the cache.

Remove(KeyValuePair<TKey, TValue>)

Removes the first occurrence of a specific key-value pair from the cache.

public bool Remove(KeyValuePair<TKey, TValue> item)

Parameters

item KeyValuePair<TKey, TValue>

The key-value pair to remove.

Returns

bool

true if the key-value pair was successfully removed; otherwise, false.

Remove(TKey)

Removes the element with the specified key from the cache.

public bool Remove(TKey key)

Parameters

key TKey

The key of the element to remove.

Returns

bool

true if the element is successfully removed; otherwise, false.

TryGetValue(TKey, out TValue)

Tries to get the value associated with the specified key. If AutoCreateObject is true and the resource is not found, it will be loaded and cached.

public bool TryGetValue(TKey key, out TValue value)

Parameters

key TKey

The key of the resource to get.

value TValue

When this method returns, contains the resource associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter.

Returns

bool

true if the cache contains an element with the specified key; otherwise, false.