Exemplo n.º 1
0
        public void WhenUsingTwoSetCacheAndMRUEvictionAfterCallingOldestAgaing()
        {
            //Setup
            var fakeHashAlgorithm = new FakeHashAlgorithm <int>();

            fakeHashAlgorithm.ObjectToHash(1, 1);
            fakeHashAlgorithm.ObjectToHash(2, 1);
            fakeHashAlgorithm.ObjectToHash(3, 1);

            var cache = new AssociativeCache <int, string>(2, 2, new LRUEvictionPolicy <int, string>(), fakeHashAlgorithm);

            cache.Add(new CacheItem <int, string>(1, "value1"));
            cache.Add(new CacheItem <int, string>(2, "value2"));
            Thread.Sleep(50);
            var value = cache.Get(1);

            //Action
            cache.Add(new CacheItem <int, string>(3, "value3"));

            //Assert
            Assert.Throws <CacheMissException>(() => cache.Get(2));
            Assert.AreSame("value3", cache.Get(3));
            Assert.AreSame("value1", value);
            Assert.AreSame("value1", cache.Get(1));
        }
Exemplo n.º 2
0
        public void TestAFullCache()
        {
            //Setup
            var fakeHashAlgorithm = new FakeHashAlgorithm <int>();

            fakeHashAlgorithm.ObjectToHash(1, 1);
            fakeHashAlgorithm.ObjectToHash(2, 2);
            fakeHashAlgorithm.ObjectToHash(3, 3);
            fakeHashAlgorithm.ObjectToHash(4, 4);
            var cache = new AssociativeCache <int, string>(2, 2, new MRUEvictionPolicy <int, string>(), fakeHashAlgorithm);

            //Action
            cache.Add(new CacheItem <int, string>(1, "value1"));
            cache.Add(new CacheItem <int, string>(2, "value2"));
            cache.Add(new CacheItem <int, string>(3, "value3"));
            cache.Add(new CacheItem <int, string>(4, "value4"));

            //Assert
            Assert.AreSame("value1", cache.Get(1));
            Assert.AreSame("value2", cache.Get(2));
            Assert.AreSame("value3", cache.Get(3));
            Assert.AreSame("value4", cache.Get(4));
        }