public void TestCacheTimeToLive_Constant() { var ttl = 3; int misses = 0; string result; var stopwatch = new Stopwatch(); Func <string> getter = () => { misses++; return(misses.ToString()); }; var expireAt = DateTime.Now.AddSeconds(ttl); stopwatch.Start(); var cache = GetCacheWithAbsoluteExpiration("constant-expire", expireAt); cache.ClearAll(); result = cache.Get("key", getter); DateTime insertTime = DateTime.Now; Assert.AreEqual(1, misses); Assert.AreEqual("1", result); result = cache.Get("key", getter); Assert.AreEqual(1, misses); Assert.AreEqual("1", result); CacheTestTools.AssertValueDoesntChangeWithin(cache, "key", "1", getter, stopwatch, ttl); // Should expire within TTL+60sec from insert CacheTestTools.AssertValueDoesChangeWithin(cache, "key", "1", getter, stopwatch, 60.1); result = cache.Get("key", getter); Assert.AreNotEqual(1, misses); Assert.AreNotEqual("1", result); }
public void TestCacheTimeToLive_FromInsert() { var ttl = 3; int misses = 0; string result; var stopwatch = new Stopwatch(); Func <string> getter = () => { misses++; return(misses.ToString()); }; var cache = GetCacheWithExpirationFromAdd("insert-expire-cache", TimeSpan.FromSeconds(ttl)); cache.ClearAll(); stopwatch.Start(); result = cache.Get("key", getter); Assert.AreEqual(1, misses); Assert.AreEqual("1", result); result = cache.Get("key", getter); Assert.AreEqual(1, misses); Assert.AreEqual("1", result); CacheTestTools.AssertValueDoesntChangeWithin(cache, "key", "1", getter, stopwatch, ttl); // Should expire within TTL+60sec from insert CacheTestTools.AssertValueDoesChangeWithin(cache, "key", "1", getter, stopwatch, 60.1); result = cache.Get("key", getter); Assert.AreNotEqual(1, misses); Assert.AreNotEqual("1", result); }