Table of Contents

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

TKey

The type of the key.

TValue

The 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

comparer IEqualityComparer<TKey>

The IEqualityComparer<T> implementation to use when comparing keys.

valueFactory Func<TKey, TValue>

Factory method that generates a new TValue for a given TKey.

valueReleaser Action<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

valueFactory Func<TKey, TValue>

Factory method that generates a new TValue for a given TKey.

valueReleaser Action<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

concurrencyLevel int

The estimated number of threads that will access the RefCountedConcurrentDictionary<TKey, TValue> concurrently

capacity int

The initial number of elements that the RefCountedConcurrentDictionary<TKey, TValue> can contain.

comparer IEqualityComparer<TKey>

The IEqualityComparer<T> implementation to use when comparing keys.

valueFactory Func<TKey, TValue>

Factory method that generates a new TValue for a given TKey.

valueReleaser Action<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

concurrencyLevel int

The estimated number of threads that will access the RefCountedConcurrentDictionary<TKey, TValue> concurrently

capacity int

The initial number of elements that the RefCountedConcurrentDictionary<TKey, TValue> can contain.

valueFactory Func<TKey, TValue>

Factory method that generates a new TValue for a given TKey.

valueReleaser Action<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

key TKey

The 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

key TKey

THe key of the element to release.