public void TestInMemoryCacheTimeToLive_FromInsert() { var ttl = 3; int misses = 0; string result; var stopwatch = new Stopwatch(); Func <string> getter = () => { misses++; return(misses.ToString()); }; var cache = new InMemoryCache( "insert-expire-cache", new InMemoryPolicy { ExpirationFromAdd = 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); }
public void TestInMemoryCacheGetTwice() { var cache = new InMemoryCache("cache1", new InMemoryPolicy()); cache.ClearAll(); int misses = 0; Func <string> getter = () => { misses++; return(misses.ToString()); }; string result; result = cache.Get("key", getter); Assert.AreEqual("1", result); result = cache.Get("key", getter); Assert.AreEqual("1", result); }
public void TestInMemoryCacheTimeToLive_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 = new InMemoryCache( "constant-expire", new InMemoryPolicy { AbsoluteExpiration = 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 TestInMemoryCacheSetTwice() { var cache = new InMemoryCache("cache1", new InMemoryPolicy()); cache.ClearAll(); int misses = 0; Func <string> getter = () => { misses++; return(misses.ToString()); }; string result; bool wasFound; cache.Set("key", getter()); wasFound = cache.TryGet("key", out result); Assert.AreEqual(true, wasFound); Assert.AreEqual("1", result); cache.Set("key", getter()); wasFound = cache.TryGet("key", out result); Assert.AreEqual(true, wasFound); Assert.AreEqual("2", result); }