public void TestTwoLevelCache() { var hundredBytes = new byte[100]; ICache primary = new MemoryCache(500, new LruCacheEvictionPolicy(), 400, 300); ICache secondary = new MemoryCache(1000, new LruCacheEvictionPolicy(), 900, 700); ICache cache = new TwoLevelCache(primary, secondary); for (int i = 0; i < 9; i++) { cache.Insert("Entry " + i, hundredBytes, CachePriority.Normal); Thread.Sleep(20); } for (int i = 0; i < 9; i++) { Assert.IsTrue(secondary.ContainsKey("Entry " + i), "Expected secondary cache to contain Entry {0} after initialization"); if (i < 5) { Assert.IsFalse(primary.ContainsKey("Entry " + i), "Expected primary cache to NOT contain Entry {0} after initialization", i); } else { Assert.IsTrue(primary.ContainsKey("Entry " + i), "Expected primary cache to contain Entry {0} after initialization", i); } } cache.Insert("Entry 10", hundredBytes, CachePriority.Normal); Assert.IsFalse(cache.ContainsKey("Entry 0")); Assert.IsFalse(cache.ContainsKey("Entry 1")); Assert.IsTrue(cache.ContainsKey("Entry 2")); Assert.IsTrue(cache.ContainsKey("Entry 10")); Assert.IsTrue(primary.ContainsKey("Entry 10")); Assert.IsTrue(secondary.ContainsKey("Entry 10")); }
public void TestTwoLevelCache() { var hundredBytes = new byte[100]; ICache primary = new MemoryCache(500, new LruCacheEvictionPolicy(), 400, 300); ICache secondary = new MemoryCache(1000, new LruCacheEvictionPolicy(), 900, 700); ICache cache = new TwoLevelCache(primary, secondary); for (int i = 0; i < 9; i++) { cache.Insert("Entry " + i, hundredBytes, CachePriority.Normal); Thread.Sleep(20); } for(int i = 0; i < 9; i++) { Assert.IsTrue(secondary.ContainsKey("Entry " + i), "Expected secondary cache to contain Entry {0} after initialization"); if (i < 5) Assert.IsFalse(primary.ContainsKey("Entry " + i), "Expected primary cache to NOT contain Entry {0} after initialization", i); else Assert.IsTrue(primary.ContainsKey("Entry " + i), "Expected primary cache to contain Entry {0} after initialization", i); } cache.Insert("Entry 10", hundredBytes, CachePriority.Normal); Assert.IsFalse(cache.ContainsKey("Entry 0")); Assert.IsFalse(cache.ContainsKey("Entry 1")); Assert.IsTrue(cache.ContainsKey("Entry 2")); Assert.IsTrue(cache.ContainsKey("Entry 10")); Assert.IsTrue(primary.ContainsKey("Entry 10")); Assert.IsTrue(secondary.ContainsKey("Entry 10")); }