Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        public void TestLayeredCacheBothRegisteredToSameInstance2()
        {
            CacheManager.RemoveCache("l");
            CacheManager.RemoveCache("l*");
            CacheManager.RemoveCache("l1");
            CacheManager.RemoveCache("l2");

            CacheManager.SetCache("l*", new Mocks.MockMemCache("l*"));

            var cache = new LayeredCache("cache0", "l1", "l2");
        }
Exemplo n.º 3
0
        public void TestLayeredCacheLevel1NotRegistered()
        {
            CacheManager.RemoveCache("l");
            CacheManager.RemoveCache("l*");
            CacheManager.RemoveCache("l1");
            CacheManager.RemoveCache("l2");

            CacheManager.SetCache("l2", new Mocks.MockMemCache("l2"));

            var cache = new LayeredCache("cache0", "l1", "l2");
        }
Exemplo n.º 4
0
        public void TestLayeredCacheNullLevel2Name()
        {
            CacheManager.RemoveCache("l");
            CacheManager.RemoveCache("l*");
            CacheManager.RemoveCache("l1");
            CacheManager.RemoveCache("l2");

            CacheManager.SetCache("l1", new Mocks.MockMemCache("l1"));
            CacheManager.SetCache("l2", new Mocks.MockMemCache("l2"));

            var cache = new LayeredCache("cache0", "l1", null);
        }
Exemplo n.º 5
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.º 6
0
 public void TestLayeredCacheLevelsEqual()
 {
     var l     = new Mocks.MockMemCache("l");
     var cache = new LayeredCache("cache0", l, l);
 }
Exemplo n.º 7
0
 public void TestLayeredCacheNullLevel2()
 {
     var cache = new LayeredCache("cache0", new Mocks.MockMemCache("l1"), null);
 }
Exemplo n.º 8
0
 public void TestLayeredCacheNullLevel1()
 {
     var cache = new LayeredCache("cache0", null, new Mocks.MockMemCache("l2"));
 }
Exemplo n.º 9
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);
        }