public void Add_Should_Replace_MRU_Item_When_Cache_Is_Full_And_MRU_Is_The_Replacement_Handler()
        {
            var mruCache = new SetAssociativeCache <CacheKey, CacheValue>(numberOfWays, numberOfSets,
                                                                          () => new MRUReplacementHandler <CacheKey, CacheValue>());

            // Arrange
            int setIndex  = 1;
            var cacheKeys = FillCacheSet(setIndex, cache);

            VisitCacheItems(new List <CacheKey>()
            {
                cacheKeys.First()
            }, mruCache);
            var mostRecentlyUsedKey = cacheKeys.First();

            // Act
            var empNumber = new CacheKey("001", setIndex);
            var empName   = new CacheValue("Steve Jobs");

            cache.Add(empNumber, empName);

            // Assert
            cache.Contains(mostRecentlyUsedKey).Should().BeFalse();
            cache.Get(empNumber).Should().Be(empName);
        }
 private static void VisitCacheItems(IEnumerable <CacheKey> keysToVisit, SetAssociativeCache <CacheKey, CacheValue> cache)
 {
     foreach (var key in keysToVisit)
     {
         cache.Get(key);
     }
 }
Пример #3
0
        /// <summary>
        /// Adds the items into2 way cache.
        /// </summary>
        private static void Test2WayCache()
        {
            SetAssociativeCache <int, int> setAssociativeCache = new SetAssociativeCache <int, int>(2, 3);

            Console.WriteLine("Adding Items into 2 Way Cache with a cache capacity of 3 and trying to add 10 items from 1 to 10 using LRU Cache Policy");

            //add items to cache
            for (int i = 1; i <= 10; i++)
            {
                setAssociativeCache.Add(i, i);
            }

            Console.WriteLine("Done Adding Items into 2 Way Cache with a cache capacity of 3 and trying to add 10 items from 1 to 10 using LRU Cache Policy");

            Console.WriteLine("Reading Items from the 2 Way Cache with a cache capacity of 3 and reading 10 items from 1 to 10 using LRU Cache Policy");

            //read items from cache and print
            for (int key = 1; key <= 10; key++)
            {
                int value;

                setAssociativeCache.TryGetValue(key, out value);

                Console.WriteLine("Key- " + key + ": " + "Value- " + value);
            }

            Console.WriteLine("Cache Sets Count: " + setAssociativeCache.Count());

            Console.WriteLine("Cache Items Count: " + setAssociativeCache.ItemsCount());

            Console.WriteLine("Done Reading Items from the 2 Way Cache with a cache capacity of 3 and reading 10 items from 1 to 10");
        }
Пример #4
0
        public void Init()
        {
            OneWayCacheCapacity = 8;
            TwoWayCacheCapacity = 3;

            OneWayNumberOfWays = 1;
            TwoWayNumberOfWays = 2;

            Set1WayAssociativeCache = new SetAssociativeCache <int, int>(OneWayNumberOfWays, OneWayCacheCapacity);

            Set2WayAssociativeCache = new SetAssociativeCache <int, int>(TwoWayNumberOfWays, TwoWayCacheCapacity);
        }
        private static List <CacheKey> FillCacheSet(int setIndex, SetAssociativeCache <CacheKey, CacheValue> cache)
        {
            List <CacheKey> keyList = new List <CacheKey>();

            cache.Clear();

            for (int i = 0; i < cache.NumberOfWays; i++)
            {
                var empNumber = new CacheKey($"{i}", setIndex);
                var empName   = new CacheValue($"Employee {i}");

                cache.Add(empNumber, empName);
                keyList.Add(empNumber);
                ;
            }

            return(keyList);
        }
Пример #6
0
        public void Add_CustomNoEvictionReplacementStrategy_DoesNotEvictWhenFull()
        {
            m_cache = new SetAssociativeCache <IKeyType, string>(m_setCount, m_numberOfWays,
                                                                 (size) => new NoEvictionAssociativeCache <string>(size));

            FillCacheAndValidate();

            List <KeyValuePair <IKeyType, string> > newValues = new List <KeyValuePair <IKeyType, string> >();

            for (int i = 0; i < m_setCount; ++i)
            {
                newValues.Add(new KeyValuePair <IKeyType, string>(new KeyType(i - m_setCount), m_rand.Next().ToString()));
            }

            newValues.ForEach(pair => m_cache.Add(pair.Key, pair.Value));

            newValues.ForEach(pair => Assert.Equal(m_cache.Get(pair.Key), null));

            Assert.Equal(m_cache.Count, m_setCount * m_numberOfWays);
        }
Пример #7
0
 public override void RegisterCache()
 {
     m_cache = new SetAssociativeCache <IKeyType, string>(m_setCount, m_numberOfWays,
                                                          () => new LRUEvictionPolicy <string>());
 }
        public void SetAssociativeCache_BasicUsage()
        {
            string myKey     = "12345";
            int    itemCount = 100;
            int    slotSize  = 100;
            SetAssociativeCache <string, string> cache = new SetAssociativeCache <string, string>(slotSize, itemCount);



            /////////////////////////////////////////
            // 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++)
            {
                string k = "Key " + i;
                string v = "Value " + i;

                cache.Add(k, v);

                if (i >= itemCount)
                {
                    //Assert.IsTrue(cache.GetCount(k) == itemCount);
                }

                cachedValue = cache.Get(k);
                Assert.IsTrue(cachedValue == v);
            }


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

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

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

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

            cache.Add("4", "Fourth");
            Assert.IsTrue(cache.GetCount("4") == 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.GetCount("4") == 2);
        }
 public void Initialize()
 {
     cache = new SetAssociativeCache <CacheKey, CacheValue>(numberOfWays, numberOfSets,
                                                            () => new LRUReplacementHandler <CacheKey, CacheValue>());
 }
Пример #10
0
 public override void RegisterCache()
 {
     m_cache = new SetAssociativeCache <IKeyType, string>(m_setCount, m_numberOfWays,
                                                          (size) => new RandomEvictionAssociativeCache <string>(size));
 }
Пример #11
0
        public void SetAssociativeCache_BasicUsage()
        {
            string myKey = "12345";
            int itemCount = 100;
            int slotSize = 100;
            SetAssociativeCache<string, string> cache = new SetAssociativeCache<string, string>(slotSize, itemCount);

            /////////////////////////////////////////
            // 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++)
            {
                string k = "Key " + i;
                string v = "Value " + i;

                cache.Add(k, v);

                if (i >= itemCount)
                {
                    //Assert.IsTrue(cache.GetCount(k) == itemCount);
                }

                cachedValue = cache.Get(k);
                Assert.IsTrue(cachedValue == v);

            }

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

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

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

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

            cache.Add("4", "Fourth");
            Assert.IsTrue(cache.GetCount("4") == 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.GetCount("4") == 2);
        }