BrianSandberg.ConcurrentAsyncCache 1.1.2
Concurrent Async Cache
Thread-safe async caching mechanism for retrieving cached values, or invoking workers to generate and cache values if they weren't already stored.
This builds on System.Collections.Concurrent.ConcurrentDictionary to add support for async workers, cancellation tokens, retries and timeouts, throttling the number of concurrent workers, and various forms of cache expiration.
A use-case for this is services or webapplications that retrieve static data from slow DB or REST API calls. By doing the requests via the memoizer, overlapping requests for the same data will be eliminated, results can be reused, and the workload be managed.
Features:
- Thread safe.
- Async or blocking workers.
- Multiple strategies for cancelling workers that don't support cancellation tokens, including executing workers in separate threads that are then abortable on systems that support Thread.Abort().
- Throttling mechanism for concurrent workers. (to-do)
- Retry mechanism for failed workers. (to-do)
- Caching with custom policies for expirations, optional updates, and stale data grace periods. (to-do)
var memoizer = new Memoizer<string>();
// Create the result with an async worker, aborting if the job takes more than 2 seconds
var result = await memoizer.GetOrAddAsync("mycalculation",
async token => {
await Task.Delay(100, token);
return 42;
},
new CancellationTokenSource(TimeSpan.FromSeconds(2)).Token);
No packages depend on BrianSandberg.ConcurrentAsyncCache.
.NET Standard 2.0
- No dependencies.