Skip to main content

Curiosus.Cache.MemoryCache

NuGet Downloads Coverage

GetOrCreate extensions for IMemoryCache that take the expiration (or MemoryCacheEntryOptions) right in the call, with sync, Task and ValueTask factories.

Installation​

dotnet add package Curiosus.Cache.MemoryCache

Usage​

using Curiosus.Cache.MemoryCache;

public class CurrencyRates
{
private readonly IMemoryCache _cache;
private readonly IRatesApi _api;

public CurrencyRates(IMemoryCache cache, IRatesApi api)
{
_cache = cache;
_api = api;
}

public Task<decimal> GetRateAsync(string currency)
{
return _cache.GetOrCreateAsync(
$"rate:{currency}",
() => _api.LoadRateAsync(currency),
TimeSpan.FromMinutes(10));
}
}

Available overloads:

  • GetOrCreate(key, Func<T>, TimeSpan | MemoryCacheEntryOptions)
  • GetOrCreateAsync(key, Func<Task<T>>, TimeSpan | MemoryCacheEntryOptions)
  • GetOrCreateValueAsync(key, Func<ValueTask<T>>, TimeSpan | MemoryCacheEntryOptions)
  • GetOrCreateValueAsync(key, Func<ICacheEntry, ValueTask<T>>) — configure the entry inside the factory

Except for the ICacheEntry overload, values are stored in an internal wrapper: read them only through these methods (not cache.Get<T>), and note that null results are not cached, so the factory runs again on the next call. Concurrent misses are not deduplicated: several callers may run the factory at the same time.

See also​