Exemple #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));
        }
Exemple #2
0
        public void AddTwoObjectToCache()
        {
            //Setup
            var cache = new AssociativeCache <string, string>(2);

            //Action
            cache.Add(new CacheItem <string, string>("key1", "value1"));
            cache.Add(new CacheItem <string, string>("asdf", "value2"));

            //Assert
            Assert.AreSame("value1", cache.Get("key1"));
            Assert.AreSame("value2", cache.Get("asdf"));
        }
Exemple #3
0
        public void WhenItemNotInCache()
        {
            //Setup
            var cache = new AssociativeCache <string, int>(1);

            //Assert
            Assert.Throws <CacheMissException>(() => cache.Get("anykey"));
        }
Exemple #4
0
        public void AssociativeCache_Remove_Ok()
        {
            AssociativeCache <int, string> cache = new AssociativeCache <int, string>(3, 6);

            cache.Add(1, "dataA");
            cache.Remove(1);
            Assert.IsNull(cache.Get(1));
        }
Exemple #5
0
        public void AddOneObjectToCache()
        {
            //Setup
            var cache = new AssociativeCache <string, string>(1);

            cache.Add(new CacheItem <string, string>("key", "value"));

            //Assert
            Assert.AreSame("value", cache.Get("key"));
        }
Exemple #6
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));
        }
Exemple #7
0
        public void AssociativeCache_Should_Evict_By_MRUPolicy()
        {
            AssociativeCache <int, string> cache = new AssociativeCache <int, string>(3, 6, new MRUPolicyFactory <int, string>());

            cache.Add(1, "dataA");
            cache.Add(2, "dataB");
            cache.Add(3, "dataC");
            cache.Add(4, "dataD");
            cache.Add(5, "dataE");
            cache.Add(6, "dataF");
            cache.Add(7, "dataAA");

            Assert.AreEqual(cache.Get(4), null);
        }
Exemple #8
0
        public void AssociativeCache_ComplexKeyIsOk()
        {
            AssociativeCache <MockKey, MockValue> cache = new AssociativeCache <MockKey, MockValue>(3, 6);
            var key = new MockKey {
                prop = "prop", prop1 = "prop1", prop2 = "prop2"
            };
            var value1 = new MockValue {
                prop = "value", prop1 = "value1", prop2 = "value2"
            };

            cache.Add(key, value1);
            Assert.AreEqual(value1, cache.Get(key));

            var key2 = new MockKey {
                prop = "prop", prop1 = "prop11"
            };
            var value2 = new MockValue {
                prop = "value", prop1 = "value11"
            };

            cache.Add(key2, value2);
            Assert.AreNotEqual(value2, cache.Get(key));
        }
Exemple #9
0
        public void MRUEvictionTest()
        {
            //Setup
            var cache = new AssociativeCache <string, string>(2, new MRUEvictionPolicy <string, string>(), new MyMd5HashAlgorithm <string>());

            cache.Add(new CacheItem <string, string>("key1", "value1"));
            Thread.Sleep(50);
            cache.Add(new CacheItem <string, string>("key2", "value2"));

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

            //Assert
            Assert.Throws <CacheMissException>(() => cache.Get("key2"));
        }
Exemple #10
0
        public void LRUEvictionTest()
        {
            //Setup
            var cache = new AssociativeCache <int, string>(2, new LRUEvictionPolicy <int, string>(), new MyMd5HashAlgorithm <int>());

            cache.Add(new CacheItem <int, string>(2, "value1"));
            Thread.Sleep(50);
            cache.Add(new CacheItem <int, string>(3, "value2"));

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

            //Assert
            Assert.Throws <CacheMissException>(() => cache.Get(2));
        }
Exemple #11
0
        public void AssociativeCache_AddAndGetTest()
        {
            AssociativeCache <string, string> cache = new AssociativeCache <string, string>(3, 24);

            cache.Add("A", "dataA");
            cache.Add("B", "dataB");
            cache.Add("C", "dataC");
            cache.Add("D", "dataD");
            cache.Add("E", "dataE");
            cache.Add("F", "dataF");

            Assert.AreEqual("dataA", cache.Get("A"));
            Assert.AreEqual("dataB", cache.Get("B"));
            Assert.AreEqual("dataC", cache.Get("C"));
            Assert.AreEqual("dataD", cache.Get("D"));
            Assert.AreEqual("dataE", cache.Get("E"));
            Assert.AreEqual("dataF", cache.Get("F"));
        }
        public void AssociativeCache_BasicUsage()
        {
            string myKey     = "12345";
            int    itemCount = 100;

            //////////////////////////////////////////
            // IMPORTANT: Choose your EvictionPolicy
            //////////////////////////////////////////
            //IEvictionPolicy<string, string> evictionPolicy = new LruEviction<string, string>();
            IEvictionPolicy <string, string> evictionPolicy = new MruEviction <string, string>();



            IAssociativeCache <string, string> cache = new AssociativeCache <string, string>(itemCount, evictionPolicy);



            /////////////////////////////////////////
            // TEST 1). Get null value
            /////////////////////////////////////////
            string cachedValue = cache.Get(myKey);

            Assert.IsTrue(string.IsNullOrWhiteSpace(cachedValue));


            //////////////////////////////////////////////////////////////////////
            // TEST 2). Add Items and ensure that items are removed when count hit
            //////////////////////////////////////////////////////////////////////
            for (int i = 1; i < itemCount + 10; i++)
            {
                cache.Add("Key" + i, "Value" + i);

                if (i >= itemCount)
                {
                    Assert.IsTrue(cache.Count == itemCount);
                }
                else
                {
                    Assert.IsTrue(cache.Count == i);
                }
            }


            //////////////////////////////////////////////////////////////////////
            // TEST 3). Add a few items and Check counts
            //////////////////////////////////////////////////////////////////////
            cache = new AssociativeCache <string, string>(2);

            cache.Add("1", "First");
            Assert.IsTrue(cache.Count == 1);

            cache.Add("2", "Second");
            Assert.IsTrue(cache.Count == 2);

            cache.Add("3", "Third");
            Assert.IsTrue(cache.Count == 2);             // NOTE: this remains the same

            cache.Add("4", "Fourth");
            Assert.IsTrue(cache.Count == 2);             // NOTE: this remains the same



            //////////////////////////////////////////////////////////////////////
            // TEST 4). Add and Get from cache
            /////////////////////////////////////////////////////////////////////
            cache.Flush();

            cache.Add("1", "First");
            Assert.IsTrue(cache.Get("1") == "First");

            cache.Add("2", "Second");
            Assert.IsTrue(cache.Get("2") == "Second");

            cache.Add("3", "Third");
            Assert.IsTrue(cache.Get("3") == "Third");

            cache.Add("4", "Fourth");
            Assert.IsTrue(cache.Get("4") == "Fourth");

            // And finally the first 2 should now be null
            Assert.IsTrue(cache.Get("1") == null);

            Assert.IsTrue(cache.Get("2") == null);

            // Should still be count 2
            Assert.IsTrue(cache.Count == 2);
        }
        public void AssociativeCache_BasicUsage()
        {
            string myKey = "12345";
            int itemCount = 100;

            //////////////////////////////////////////
            // IMPORTANT: Choose your EvictionPolicy
            //////////////////////////////////////////
            //IEvictionPolicy<string, string> evictionPolicy = new LruEviction<string, string>();
            IEvictionPolicy<string, string> evictionPolicy = new MruEviction<string, string>();

            IAssociativeCache<string, string> cache = new AssociativeCache<string, string>(itemCount, evictionPolicy);

            /////////////////////////////////////////
            // TEST 1). Get null value
            /////////////////////////////////////////
            string cachedValue = cache.Get(myKey);
            Assert.IsTrue(string.IsNullOrWhiteSpace(cachedValue));

            //////////////////////////////////////////////////////////////////////
            // TEST 2). Add Items and ensure that items are removed when count hit
            //////////////////////////////////////////////////////////////////////
            for ( int i = 1; i < itemCount+10; i++)
            {
                cache.Add("Key" + i, "Value" + i);

                if(i >= itemCount)
                {
                    Assert.IsTrue(cache.Count == itemCount);
                }
                else
                {
                    Assert.IsTrue(cache.Count == i);
                }
            }

            //////////////////////////////////////////////////////////////////////
            // TEST 3). Add a few items and Check counts
            //////////////////////////////////////////////////////////////////////
            cache = new AssociativeCache<string, string>(2);

            cache.Add("1", "First");
            Assert.IsTrue(cache.Count == 1);

            cache.Add("2", "Second");
            Assert.IsTrue(cache.Count == 2);

            cache.Add("3", "Third");
            Assert.IsTrue(cache.Count == 2); // NOTE: this remains the same

            cache.Add("4", "Fourth");
            Assert.IsTrue(cache.Count == 2); // NOTE: this remains the same

            //////////////////////////////////////////////////////////////////////
            // TEST 4). Add and Get from cache
            /////////////////////////////////////////////////////////////////////
            cache.Flush();

            cache.Add("1", "First");
            Assert.IsTrue(cache.Get("1") == "First");

            cache.Add("2", "Second");
            Assert.IsTrue(cache.Get("2") == "Second");

            cache.Add("3", "Third");
            Assert.IsTrue(cache.Get("3") == "Third");

            cache.Add("4", "Fourth");
            Assert.IsTrue(cache.Get("4") == "Fourth");

            // And finally the first 2 should now be null
            Assert.IsTrue(cache.Get("1") == null);

            Assert.IsTrue(cache.Get("2") == null);

            // Should still be count 2
            Assert.IsTrue(cache.Count == 2);
        }