Class RefCountedConcurrentDictionary<TKey, TValue>
- Namespace
- SixLabors.ImageSharp.Web.Synchronization
- Assembly
- SixLabors.ImageSharp.Web.dll
Represents a thread-safe collection of reference-counted key/value pairs that can be accessed by multiple threads concurrently. Values that don't yet exist are automatically created using a caller supplied value factory method, and when their final refcount is released they are removed from the dictionary.
public class RefCountedConcurrentDictionary<TKey, TValue> where TKey : notnull where TValue : class
Type Parameters
TKeyThe type of the key.
TValueThe value for the dictionary.
- Inheritance
-
RefCountedConcurrentDictionary<TKey, TValue>
- Inherited Members
Constructors
RefCountedConcurrentDictionary(IEqualityComparer<TKey>, Func<TKey, TValue>, Action<TValue>?)
Initializes a new instance of the RefCountedConcurrentDictionary<TKey, TValue> class that is empty, has the default concurrency level and capacity,, and uses the specified IEqualityComparer<T>.
public RefCountedConcurrentDictionary(IEqualityComparer<TKey> comparer, Func<TKey, TValue> valueFactory, Action<TValue>? valueReleaser)
Parameters
comparerIEqualityComparer<TKey>The IEqualityComparer<T> implementation to use when comparing keys.
valueFactoryFunc<TKey, TValue>Factory method that generates a new
TValuefor a givenTKey.valueReleaserAction<TValue>Optional callback that is used to cleanup
TValues after their final ref count is released.
RefCountedConcurrentDictionary(Func<TKey, TValue>, Action<TValue>?)
Initializes a new instance of the RefCountedConcurrentDictionary<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.
public RefCountedConcurrentDictionary(Func<TKey, TValue> valueFactory, Action<TValue>? valueReleaser = null)
Parameters
valueFactoryFunc<TKey, TValue>Factory method that generates a new
TValuefor a givenTKey.valueReleaserAction<TValue>Optional callback that is used to cleanup
TValues after their final ref count is released.
RefCountedConcurrentDictionary(int, int, IEqualityComparer<TKey>, Func<TKey, TValue>, Action<TValue>?)
Initializes a new instance of the RefCountedConcurrentDictionary<TKey, TValue> class that is empty, has the specified concurrency level, has the specified initial capacity, and uses the specified IEqualityComparer<T>.
public RefCountedConcurrentDictionary(int concurrencyLevel, int capacity, IEqualityComparer<TKey> comparer, Func<TKey, TValue> valueFactory, Action<TValue>? valueReleaser)
Parameters
concurrencyLevelintThe estimated number of threads that will access the RefCountedConcurrentDictionary<TKey, TValue> concurrently
capacityintThe initial number of elements that the RefCountedConcurrentDictionary<TKey, TValue> can contain.
comparerIEqualityComparer<TKey>The IEqualityComparer<T> implementation to use when comparing keys.
valueFactoryFunc<TKey, TValue>Factory method that generates a new
TValuefor a givenTKey.valueReleaserAction<TValue>Optional callback that is used to cleanup
TValues after their final ref count is released.
RefCountedConcurrentDictionary(int, int, Func<TKey, TValue>, Action<TValue>?)
Initializes a new instance of the RefCountedConcurrentDictionary<TKey, TValue> class that is empty, has the specified concurrency level and capacity, and uses the default comparer for the key type.
public RefCountedConcurrentDictionary(int concurrencyLevel, int capacity, Func<TKey, TValue> valueFactory, Action<TValue>? valueReleaser = null)
Parameters
concurrencyLevelintThe estimated number of threads that will access the RefCountedConcurrentDictionary<TKey, TValue> concurrently
capacityintThe initial number of elements that the RefCountedConcurrentDictionary<TKey, TValue> can contain.
valueFactoryFunc<TKey, TValue>Factory method that generates a new
TValuefor a givenTKey.valueReleaserAction<TValue>Optional callback that is used to cleanup
TValues after their final ref count is released.
Methods
Get(TKey)
Obtains a reference to the value corresponding to the specified key. If no such value exists in the dictionary, then a new value is generated using the value factory method supplied in the constructor. To prevent leaks, this reference MUST be released via Release(TKey).
public TValue Get(TKey key)
Parameters
keyTKeyThe key of the element to add ref.
Returns
- TValue
The referenced object.
Release(TKey)
Releases a reference to the value corresponding to the specified key. If this reference was the last remaining reference to the value, then the value is removed from the dictionary, and the optional value releaser callback is invoked.
public void Release(TKey key)
Parameters
keyTKeyTHe key of the element to release.