public void BackgroundCleanupIsInitiatedWhenIntervalIsSet() { CacheOptions options = CacheOptions.SlidingExpiration(new TimeSpan(0, 0, 0, 0, 50)); // Initially values are not getting purged because the interval is 0. Cache.Set("key", "value", options); Thread.Sleep(300); Assert.IsTrue(Cache.ContainsKey("key")); // Cleanup manually to demonstrate that the value should go away. Cache.Cleanup(); Assert.IsFalse(Cache.ContainsKey("key")); // Now enable auto-cleanup Cache.CleanupIntervalInMilliseconds = 100; Assert.AreEqual(100, Cache.CleanupIntervalInMilliseconds); // Add some values and watch them get purged periodically. Cache.Set("key", "value", options); Thread.Sleep(300); Assert.IsFalse(Cache.ContainsKey("key")); Cache.Set("key", "value", options); Thread.Sleep(300); Assert.IsFalse(Cache.ContainsKey("key")); // Now stop the cleanup and notice that they don't get purged anymore. Cache.CleanupIntervalInMilliseconds = 0; Cache.Set("key", "value", options); Thread.Sleep(300); Assert.IsTrue(Cache.ContainsKey("key")); }
public void CleanupPurgesExpiredEntries() { Cache.Set("key1", "value1", CacheOptions.AbsoluteExpiration(DateTime.UtcNow)); Cache.Set("key2", "value2", CacheOptions.AbsoluteExpiration(DateTime.MinValue)); Cache.Set("key3", "value3", CacheOptions.AbsoluteExpiration(DateTime.MaxValue)); Cache.Set("key4", "value4", CacheOptions.SlidingExpiration(TimeSpan.Zero)); Cache.Set("key5", "value5", CacheOptions.NoExpiration); Thread.Sleep(100); // Cause keys to expire Cache.Cleanup(); // Note: Order of ContainsKey / Get matters because ContainsKey // doesn't auto-expire values like Get does. Assert.IsFalse(Cache.ContainsKey("key1")); Assert.AreEqual(null, Cache.Get("key1")); Assert.IsFalse(Cache.ContainsKey("key2")); Assert.AreEqual(null, Cache.Get("key2")); Assert.IsTrue(Cache.ContainsKey("key3")); Assert.AreEqual("value3", Cache.Get("key3")); Assert.IsFalse(Cache.ContainsKey("key4")); Assert.AreEqual(null, Cache.Get("key4")); Assert.IsTrue(Cache.ContainsKey("key5")); Assert.AreEqual("value5", Cache.Get("key5")); }
public void SlidingExpirationFactory() { CacheOptions options = CacheOptions.SlidingExpiration(new TimeSpan(1, 2, 3)); Assert.AreEqual(new TimeSpan(1, 2, 3), options.SlidingExpirationTimeSpan); Assert.IsNull(options.AbsoluteExpirationTime); Assert.AreEqual(0, options.CustomOptions.Count); }