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
TKeyThe type of the keys used to identify resources.
TValueThe 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
loaderCachedLoader<TKey, TValue>.TryLoadValueDelegateThe 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
loaderCachedLoader<TKey, TValue>.TryLoadValueDelegateThe function used to load resources.
cacheHolderIDictionary<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
Count
Gets the number of elements contained in the cache.
public int Count { get; }
Property Value
IsReadOnly
Gets a value indicating whether the cache is read-only.
public bool IsReadOnly { get; }
Property Value
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
keyTKeyThe 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
itemKeyValuePair<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
keyTKeyThe key of the element to add.
valueTValueThe 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
itemKeyValuePair<TKey, TValue>The key-value pair to locate in the cache.
Returns
ContainsKey(TKey)
Determines whether the cache contains an element with the specified key.
public bool ContainsKey(TKey key)
Parameters
keyTKeyThe key to locate in the cache.
Returns
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
arrayKeyValuePair<TKey, TValue>[]The one-dimensional array that is the destination of the elements copied from the cache.
arrayIndexintThe 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
itemKeyValuePair<TKey, TValue>The key-value pair to remove.
Returns
Remove(TKey)
Removes the element with the specified key from the cache.
public bool Remove(TKey key)
Parameters
keyTKeyThe key of the element to remove.
Returns
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
keyTKeyThe key of the resource to get.
valueTValueWhen 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.