public async Task GetOrAddAsyncWithImmediateExpirationAndCallbackInTheDelegateDoesExpireItemsAndFireTheCallback() { var millisecondsCacheDuration = 1000; var callbackHasFired = false; var validResult = await sut.GetOrAddAsync( TestKey, entry => { return(Task.FromResult(new ComplexTestObject())); }, LazyCacheEntryOptions .WithImmediateAbsoluteExpiration(TimeSpan.FromMilliseconds(millisecondsCacheDuration)) .RegisterPostEvictionCallback((key, value, reason, state) => callbackHasFired = true)); // trigger expiry Thread.Sleep(TimeSpan.FromMilliseconds(millisecondsCacheDuration + 1000)); Assert.That(validResult, Is.Not.Null); Assert.That(callbackHasFired, Is.True); }
public async Task AutoRefresh() { var key = "someKey"; var refreshInterval = TimeSpan.FromSeconds(1); var timesGenerated = 0; // this is the Func what we are caching ComplexTestObject GetStuff() { timesGenerated++; return(new ComplexTestObject()); } // this sets up options that will recreate the entry on eviction MemoryCacheEntryOptions GetOptions() { var options = new LazyCacheEntryOptions() .SetAbsoluteExpiration(refreshInterval, ExpirationMode.ImmediateEviction); options.RegisterPostEvictionCallback((keyEvicted, value, reason, state) => { if (reason == EvictionReason.Expired || reason == EvictionReason.TokenExpired) { sut.GetOrAdd(key, _ => GetStuff(), GetOptions()); } }); return(options); } for (var i = 0; i < 3; i++) { var thing = sut.GetOrAdd(key, () => GetStuff(), GetOptions()); Assert.That(thing, Is.Not.Null); await Task.Delay(2 *refreshInterval); } // refreshed every second in 6 seconds so generated 6 times // even though we only fetched it every other second which would be 3 times Assert.That(timesGenerated, Is.EqualTo(6)); }