Exemplo n.º 1
0
        public void TestLayeredNoCacheObject()
        {
            var level1Cache = new NoCache();
            var level2Cache = new NoCache();
            var cache       = new LayeredCache("lc", level1Cache, level2Cache);

            int hits = 0;

            Func <string> getter = () => { hits++; return(hits.ToString()); };

            string result;

            result = cache.Get("key", getter);
            Assert.AreEqual(1, hits);
            Assert.AreEqual("1", result);

            result = cache.Get("key", getter);
            Assert.AreEqual(2, hits);
            Assert.AreEqual("2", result);
        }
Exemplo n.º 2
0
        public void TestLayeredCacheValidInnerCaches()
        {
            var l1 = new Mocks.MockMemCache("l1");
            var l2 = new Mocks.MockMemCache("l2");

            var cache = new LayeredCache("cache0", l1, l2);

            int hits = 0;

            Func <int> getter = () => { hits++; return(hits); };

            int result;

            result = cache.Get("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 = cache.Get("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 = cache.Get("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 = cache.Get("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 = cache.Get("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 = cache.Get("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 = cache.Get("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 = cache.Get("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 = cache.Get("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 = cache.Get("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);
        }