Class AsyncMemoizationDictionary<TInput, TOutput>
Encapsulates a memoization dictionary for caching outputs of an async function.
public sealed class AsyncMemoizationDictionary<TInput, TOutput> where TInput : notnull
Type Parameters
TInputTOutput
- Inheritance
-
AsyncMemoizationDictionary<TInput, TOutput>
- Inherited Members
- Extension Methods
Remarks
This uses a ConcurrentDictionary<TKey, TValue> under the hood, which is initialized with a given concurrency level and capacity in its constructor.
Constructors
AsyncMemoizationDictionary(Func<TInput, ValueTask<TOutput>>, int, int)
Encapsulates a memoization dictionary for caching outputs of an async function.
public AsyncMemoizationDictionary(Func<TInput, ValueTask<TOutput>> func, int concurrencyLevel, int capacity = 64)
Parameters
funcFunc<TInput, ValueTask<TOutput>>The function that computes the outputs.
concurrencyLevelintThe concurrency level to use for the cache dictionary, which should match the expected average concurrent readers of the cache. This matches the parameter in the constructor of ConcurrentDictionary<TKey, TValue>.
capacityintThe initial capacity of the dictionary.
Remarks
This uses a ConcurrentDictionary<TKey, TValue> under the hood, which is initialized with a given concurrency level and capacity in its constructor.
Methods
Clear()
Clears the cache.
public void Clear()
Get(TInput)
Gets the value of the given input. If the value has not been computed before, it is computed and stored into the dictionary for future retrievals. Otherwise, it's fetched directly from the dictionary.
public ValueTask<TOutput> Get(TInput input)
Parameters
inputTInputThe input to the function.
Returns
- ValueTask<TOutput>
The output of the function that was initialized, either cached or computed.