BrianSandberg.ConcurrentAsyncCache 1.1.5

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 database or REST API calls. By doing the requests via the cache, 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.
var cache = new Cache<string>();

// Create the result with an async worker, aborting if the job takes more than 2 seconds
var result = await cache.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.

Version Downloads Last updated
1.1.5 18 08/22/2024
1.1.4 20 08/05/2024
1.1.3 23 08/01/2024
1.1.2 22 07/31/2024
1.0.12 257 04/02/2022