public void DistributedCache_Sliding_Expire() { var distributedCache = GetDistributedCache(); var key = Guid.NewGuid().ToString("N"); var options = CacheEntryOptionsFactory.Sliding().Timeout(TimeSpan.FromMilliseconds(500)); distributedCache.Set(key, "something", options); Thread.Sleep(400); Assert.Equal("something", distributedCache.Get <string>(key)); Thread.Sleep(400); Assert.Equal("something", distributedCache.Get <string>(key)); Thread.Sleep(600); Assert.Null(distributedCache.Get <string>(key)); options = CacheEntryOptionsFactory.Sliding().Timeout(TimeSpan.FromSeconds(2)); distributedCache.Set(key, "something", options); Thread.Sleep(1800); Assert.Equal("something", distributedCache.Get <string>(key)); Thread.Sleep(1800); Assert.Equal("something", distributedCache.Get <string>(key)); Thread.Sleep(2200); Assert.Null(distributedCache.Get <string>(key)); }
public void MemoryCache_Sliding_Expire() { var memoryCache = MemoryCacheProvider.Default; var key = Guid.NewGuid().ToString("N"); var options = CacheEntryOptionsFactory.Sliding().Timeout(TimeSpan.FromMilliseconds(300)); memoryCache.Set(key, "something", options); Thread.Sleep(250); Assert.Equal("something", memoryCache.Get <string>(key)); Thread.Sleep(250); Assert.Equal("something", memoryCache.Get <string>(key)); Thread.Sleep(300); Assert.Null(memoryCache.Get <string>(key)); options = CacheEntryOptionsFactory.Sliding().Timeout(TimeSpan.FromSeconds(2)); memoryCache.Set(key, "something", options); Thread.Sleep(1800); Assert.Equal("something", memoryCache.Get <string>(key)); Thread.Sleep(1800); Assert.Equal("something", memoryCache.Get <string>(key)); Thread.Sleep(2000); Assert.Null(memoryCache.Get <string>(key)); }
public async Task DistributedCache_Sliding_Expire_Async() { var distributedCache = GetDistributedCache(); var key = Guid.NewGuid().ToString("N"); var options = CacheEntryOptionsFactory.Sliding().Timeout(TimeSpan.FromMilliseconds(1000)); await distributedCache.SetAsync(key, "something", options).ConfigureAwait(false); Thread.Sleep(800); Assert.Equal("something", await distributedCache.GetAsync <string>(key).ConfigureAwait(false)); Thread.Sleep(800); Assert.Equal("something", await distributedCache.GetAsync <string>(key).ConfigureAwait(false)); Thread.Sleep(1200); Assert.Null(await distributedCache.GetAsync <string>(key).ConfigureAwait(false)); options = CacheEntryOptionsFactory.Sliding().Timeout(TimeSpan.FromSeconds(2)); await distributedCache.SetAsync(key, "something", options).ConfigureAwait(false); Thread.Sleep(1800); Assert.Equal("something", await distributedCache.GetAsync <string>(key).ConfigureAwait(false)); Thread.Sleep(1800); Assert.Equal("something", await distributedCache.GetAsync <string>(key).ConfigureAwait(false)); Thread.Sleep(2200); Assert.Null(await distributedCache.GetAsync <string>(key).ConfigureAwait(false)); }