Class SingleCreationValueCache<TKey, TValue>
A dictionary-like type intended for thread-safe value caches whose values are created only once ever.
Inheritance
Inherited Members
Namespace: IPA.Utilities.Async
Assembly: IPA.Loader.dll
Syntax
public class SingleCreationValueCache<TKey, TValue>
Type Parameters
Name | Description |
---|---|
TKey | the key type of the cache |
TValue | the value type of the cache |
Remarks
This object basically wraps a System.Collections.Concurrent.ConcurrentDictionary`2 with some special handling to ensure that values are only created once ever, without having multiple parallel constructions.
Constructors
| Improve this Doc View SourceSingleCreationValueCache()
Initializes a new instance of the SingleCreationValueCache<TKey, TValue> class that is empty, has the default concurrency level, has the default initial capacity, and uses the default comparer for the key type.
Declaration
public SingleCreationValueCache()
SingleCreationValueCache(IEnumerable<KeyValuePair<TKey, TValue>>)
Initializes a new instance of the SingleCreationValueCache<TKey, TValue> class that contains elements copied from the specified System.Collections.Generic.IEnumerable<T>, has the default concurrency level, has the default initial capacity, and uses the default comparer for the key type.
Declaration
public SingleCreationValueCache(IEnumerable<KeyValuePair<TKey, TValue>> collection)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey, TValue>> | collection | the System.Collections.Generic.IEnumerable<T> whose element are to be used for the new cache |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | when any arguments are null |
System.ArgumentException |
|
SingleCreationValueCache(IEnumerable<KeyValuePair<TKey, TValue>>, IEqualityComparer<TKey>)
Initializes a new instance of the SingleCreationValueCache<TKey, TValue> class that contains elements copied from the specified System.Collections.Generic.IEnumerable<T> has the default concurrency level, has the default initial capacity, and uses the specified System.Collections.Generic.IEqualityComparer<T>.
Declaration
public SingleCreationValueCache(IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey, TValue>> | collection | the System.Collections.Generic.IEnumerable<T> whose elements are to be used for the new cache |
System.Collections.Generic.IEqualityComparer<TKey> | comparer | the equality comparer to use when comparing keys |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
|
SingleCreationValueCache(IEqualityComparer<TKey>)
Initializes a new instance of the SingleCreationValueCache<TKey, TValue> class that is empty, has the default concurrency level and capacity, and uses the specified System.Collections.Generic.IEqualityComparer<T>.
Declaration
public SingleCreationValueCache(IEqualityComparer<TKey> comparer)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEqualityComparer<TKey> | comparer | the equality comparer to use when comparing keys |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
|
Properties
| Improve this Doc View SourceCount
Gets the number of elements that this cache contains.
Declaration
public int Count { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
IsEmpty
Gets a value that indicates whether this cache is empty.
Declaration
public bool IsEmpty { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
Methods
| Improve this Doc View SourceClear()
Clears the cache.
Declaration
public void Clear()
ContainsKey(TKey)
Gets a value indicating whether or not this cache contains key
.
Declaration
public bool ContainsKey(TKey key)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | the key to search for |
Returns
Type | Description |
---|---|
System.Boolean | true if the cache contains the key, false otherwise |
GetOrAdd(TKey, Func<TKey, TValue>)
Gets the value associated with the specified key from the cache. If it does not exist, and no creators are currently running for this key, then the creator is called to create the value and the value is added to the cache. If there is a creator currently running for the key, then this waits for the creator to finish and retrieves the value.
Declaration
public TValue GetOrAdd(TKey key, Func<TKey, TValue> creator)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | the key to search for |
System.Func<TKey, TValue> | creator | the delegate to use to create the value if it does not exist |
Returns
Type | Description |
---|---|
TValue | the value that was found, or the result of |
ToArray()
Copies the key-value pairs stored by the cache to a new array, filtering all elements that are currently being created.
Declaration
public KeyValuePair<TKey, TValue>[] ToArray()
Returns
Type | Description |
---|---|
System.Collections.Generic.KeyValuePair<TKey, TValue>[] | an array containing a snapshot of the key-value pairs contained in this cache |
TryGetValue(TKey, out TValue)
Attempts to get the value associated with the specified key from the cache.
Declaration
public bool TryGetValue(TKey key, out TValue value)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | the key to search for |
TValue | value | the value retrieved, if any |
Returns
Type | Description |
---|---|
System.Boolean | true if the value was found, false otherwise |