public async Task TestLayeredNoCacheObjectAsync() { var level1Cache = new NoCache(); var level2Cache = new NoCache(); var cache = new LayeredCache("lc", level1Cache, level2Cache); int hits = 0; Func <Task <string> > getter = async() => { await Task.Delay(10); hits++; return(hits.ToString()); }; string result; result = await cache.GetAsync("key", getter); Assert.AreEqual(1, hits); Assert.AreEqual("1", result); result = await cache.GetAsync("key", getter); Assert.AreEqual(2, hits); Assert.AreEqual("2", result); }
public async Task TestLayeredCacheValidInnerCachesAsync() { var l1 = new Mocks.MockMemCache("l1"); var l2 = new Mocks.MockMemCache("l2"); var cache = new LayeredCache("cache0", l1, l2); int hits = 0; Func <Task <int> > getter = async() => { hits++; return(hits); }; int result; result = await cache.GetAsync("key", getter); Assert.AreEqual(1, hits); Assert.AreEqual(1, result); Assert.AreEqual(2, l1.Misses); Assert.AreEqual(2, l2.Misses); Assert.AreEqual(0, l1.Hits); Assert.AreEqual(0, l2.Hits); result = await cache.GetAsync("key", getter); Assert.AreEqual(1, hits); Assert.AreEqual(1, result); Assert.AreEqual(2, l1.Misses); Assert.AreEqual(2, l2.Misses); Assert.AreEqual(1, l1.Hits); Assert.AreEqual(0, l2.Hits); l1.ClearAll(false); result = await cache.GetAsync("key", getter); Assert.AreEqual(1, hits); Assert.AreEqual(1, result); Assert.AreEqual(4, l1.Misses); Assert.AreEqual(2, l2.Misses); Assert.AreEqual(1, l1.Hits); Assert.AreEqual(1, l2.Hits); result = await cache.GetAsync("key", getter); Assert.AreEqual(1, hits); Assert.AreEqual(1, result); Assert.AreEqual(4, l1.Misses); Assert.AreEqual(2, l2.Misses); Assert.AreEqual(2, l1.Hits); Assert.AreEqual(1, l2.Hits); l2.ClearAll(false); result = await cache.GetAsync("key", getter); Assert.AreEqual(1, hits); Assert.AreEqual(1, result); Assert.AreEqual(4, l1.Misses); Assert.AreEqual(2, l2.Misses); Assert.AreEqual(3, l1.Hits); Assert.AreEqual(1, l2.Hits); result = await cache.GetAsync("key", getter); Assert.AreEqual(1, hits); Assert.AreEqual(1, result); Assert.AreEqual(4, l1.Misses); Assert.AreEqual(2, l2.Misses); Assert.AreEqual(4, l1.Hits); Assert.AreEqual(1, l2.Hits); l2.ClearAll(false); l1.ClearAll(false); result = await cache.GetAsync("key", getter); Assert.AreEqual(2, hits); Assert.AreEqual(2, result); Assert.AreEqual(6, l1.Misses); Assert.AreEqual(4, l2.Misses); Assert.AreEqual(4, l1.Hits); Assert.AreEqual(1, l2.Hits); result = await cache.GetAsync("key", getter); Assert.AreEqual(2, hits); Assert.AreEqual(2, result); Assert.AreEqual(6, l1.Misses); Assert.AreEqual(4, l2.Misses); Assert.AreEqual(5, l1.Hits); Assert.AreEqual(1, l2.Hits); // Clears counters too cache.ClearAll(); result = await cache.GetAsync("key", getter); Assert.AreEqual(3, hits); Assert.AreEqual(3, result); Assert.AreEqual(2, l1.Misses); Assert.AreEqual(2, l2.Misses); Assert.AreEqual(0, l1.Hits); Assert.AreEqual(0, l2.Hits); result = await cache.GetAsync("key", getter); Assert.AreEqual(3, hits); Assert.AreEqual(3, result); Assert.AreEqual(2, l1.Misses); Assert.AreEqual(2, l2.Misses); Assert.AreEqual(1, l1.Hits); Assert.AreEqual(0, l2.Hits); }