/// <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"); }
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); }
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); }
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 Add_Should_Persist_Cache_Item() { // Arrange var empNumber = new CacheKey("001"); var empName = new CacheValue("Steve Jobs"); // Act cache.Add(empNumber, empName); // Assert cache.Count.Should().Be(1); }
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); }