A thread-safe dictionary that lazily initializes values asynchronously with automatic disposal
$ dotnet add package Soenneker.Dictionaries.AsyncLazy
Soenneker.Dictionaries.AsyncLazyA high-performance asynchronous lazy-loading dictionary in C#, designed to ensure only one factory execution per key while supporting concurrent access.
dotnet add package Soenneker.Dictionaries.AsyncLazy
var dictionary = new AsyncLazyDictionary<string, string>();
string result = await dictionary.Get("greeting", async () => {
await Task.Delay(1000, token); // Simulate data fetching
return "Hello, World!";
});
Console.WriteLine(result); // "Hello, World!"
await dictionary.Remove("greeting");
await dictionary.DisposeAsync();