Extension methods for ASP.NET Core MemoryCache, to support automatic, thread-safe lazy initialization of cache entries.
Install-Package MemCacheAutoReload -Version 1.2.0
Task<T> GetOrAddAutoReloadAsync<T>(string key, Func<Task<T>> valueProvider, TimeSpan refreshInterval)
IMemoryCache memCache = ...;
var result = await memCache.GetOrAddAutoReloadAsync("key", async() =>
{
await Task.Delay(2000); //Simulate some delay
return Environment.TickCount; //Return result
}, TimeSpan.FromSeconds(2)); //Refresh(call value provider every two seconds)
T GetOrAddAutoReloadAsync<T>(string key, Action<T> valueProvider, TimeSpan refreshInterval)
IMemoryCache memCache = ...;
var result = memCache.GetOrAddAutoReloadAsync("key", () =>
{
Thread.Sleep(2000); //Simulate some delay
return Environment.TickCount; //Return result
}, TimeSpan.FromSeconds(2)); //Refresh(call value provider every two seconds)
The GetOrAddAutoReaload/GetOrAddAutoReloadAsync are thread safe. The value generated is cached, and only one thread at a time executes the value provider. The cached value is returned in case a thread executes the value provider to refresh the current value, so there is no blocking except the first time, where no cached value if available.
memCache.SetAutoReload("testKey", () = > "a test value", TimeSpan.FromSeconds(2));
memCache.RemoveAutoReload("testKey");