public void AddWithTagsTest() { var provider = new MemoryCacheProvider(); string key = "AddTest" + DateTime.Now.Ticks; string[] tags = new[] { "a", "b" }; var cacheKey = new CacheKey(key, tags); var value = "Test Value " + DateTime.Now; var cachePolicy = new CachePolicy(); bool result = provider.Add(cacheKey, value, cachePolicy); result.Should().BeTrue(); // look in underlying MemoryCache string innerKey = MemoryCacheProvider.GetKey(cacheKey); var cachedValue = MemoryCache.Default.Get(innerKey); cachedValue.Should().NotBeNull(); cachedValue.Should().Be(value); // make sure cache key is in underlying MemoryCache var cacheTag = new CacheTag("a"); string tagKey = MemoryCacheProvider.GetTagKey(cacheTag); tagKey.Should().NotBeNullOrEmpty(); var cachedTag = MemoryCache.Default.Get(tagKey); cachedTag.Should().NotBeNull(); }
public void AddWithExistingTagTest() { var provider = new MemoryCacheProvider(); string key = "AddTest" + DateTime.Now.Ticks; string[] tags = new[] { "a", "b" }; var cacheKey = new CacheKey(key, tags); var value = "Test Value " + DateTime.Now; var cachePolicy = new CachePolicy(); bool result = provider.Add(cacheKey, value, cachePolicy); result.Should().BeTrue(); // make sure cache key is in underlying MemoryCache var cacheTag = new CacheTag("a"); string tagKey = MemoryCacheProvider.GetTagKey(cacheTag); tagKey.Should().NotBeNullOrEmpty(); var cachedTag = MemoryCache.Default.Get(tagKey); cachedTag.Should().NotBeNull(); // add second value with same tag string key2 = "AddTest2" + DateTime.Now.Ticks; string[] tags2 = new[] { "a", "c" }; var cacheKey2 = new CacheKey(key2, tags2); var value2 = "Test Value 2 " + DateTime.Now; var cachePolicy2 = new CachePolicy(); bool result2 = provider.Add(cacheKey2, value2, cachePolicy2); result2.Should().BeTrue(); // tag 'a' should have same value var cachedTag2 = MemoryCache.Default.Get(tagKey); cachedTag2.Should().NotBeNull(); cachedTag2.Should().Be(cachedTag); }
public void CreateChangeMonitorTest() { string key = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture); string[] tags = new[] { "a", "b" }; var cacheKey = new CacheKey(key, tags); cacheKey.Should().NotBeNull(); var monitor = MemoryCacheProvider.CreateChangeMonitor(cacheKey); monitor.Should().NotBeNull(); monitor.CacheKeys.Should().HaveCount(2); var cacheTag = new CacheTag("a"); string tagKey = MemoryCacheProvider.GetTagKey(cacheTag); tagKey.Should().NotBeNullOrEmpty(); monitor.CacheKeys.Should().Contain(tagKey); }
public void ExpireTest() { var cache = MemoryCache.Default; // purge all values foreach (KeyValuePair <string, object> pair in cache) { cache.Remove(pair.Key); } var provider = new MemoryCacheProvider(); string key = "AddTest" + DateTime.Now.Ticks; var tags = new[] { "a", "b" }; var cacheKey = new CacheKey(key, tags); var value = "Test Value " + DateTime.Now; var cachePolicy = new CachePolicy(); bool result = provider.Add(cacheKey, value, cachePolicy); result.Should().BeTrue(); // add second value with same tag string key2 = "AddTest2" + DateTime.Now.Ticks; var tags2 = new[] { "a", "c" }; var cacheKey2 = new CacheKey(key2, tags2); var value2 = "Test Value 2 " + DateTime.Now; var cachePolicy2 = new CachePolicy(); bool result2 = provider.Add(cacheKey2, value2, cachePolicy2); result2.Should().BeTrue(); // add third value with same tag string key3 = "AddTest3" + DateTime.Now.Ticks; var tags3 = new[] { "b", "c" }; var cacheKey3 = new CacheKey(key3, tags3); var value3 = "Test Value 3 " + DateTime.Now; var cachePolicy3 = new CachePolicy(); bool result3 = provider.Add(cacheKey3, value3, cachePolicy3); result3.Should().BeTrue(); var cacheTag = new CacheTag("a"); string tagKey = MemoryCacheProvider.GetTagKey(cacheTag); tagKey.Should().NotBeNullOrEmpty(); // underlying cache cache.GetCount().Should().Be(6); var cachedTag = cache.Get(tagKey); cachedTag.Should().NotBeNull(); // expire actually just changes the value for tag key provider.Expire(cacheTag); var expiredTag = cache.Get(tagKey); expiredTag.Should().NotBeNull(); expiredTag.Should().NotBe(cachedTag); // items should have been removed var expiredValue = provider.Get(cacheKey); expiredValue.Should().BeNull(); var expiredValue2 = provider.Get(cacheKey2); expiredValue2.Should().BeNull(); var expiredValue3 = provider.Get(cacheKey3); expiredValue3.Should().NotBeNull(); cache.GetCount().Should().Be(4); }
public void ExpireTest() { var cacheManager = new CacheManager(); string key = "AddTest" + DateTime.Now.Ticks; var tags = new[] { "a", "b" }; var cacheKey = new CacheKey(key, tags); var value = "Test Value " + DateTime.Now; var cachePolicy = new CachePolicy(); bool result = cacheManager.Add(cacheKey, value, cachePolicy); result.Should().BeTrue(); // add second value with same tag string key2 = "AddTest2" + DateTime.Now.Ticks; var tags2 = new[] { "a", "c" }; var cacheKey2 = new CacheKey(key2, tags2); var value2 = "Test Value 2 " + DateTime.Now; var cachePolicy2 = new CachePolicy(); bool result2 = cacheManager.Add(cacheKey2, value2, cachePolicy2); result2.Should().BeTrue(); // add third value with same tag string key3 = "AddTest3" + DateTime.Now.Ticks; var tags3 = new[] { "b", "c" }; var cacheKey3 = new CacheKey(key3, tags3); var value3 = "Test Value 3 " + DateTime.Now; var cachePolicy3 = new CachePolicy(); bool result3 = cacheManager.Add(cacheKey3, value3, cachePolicy3); result3.Should().BeTrue(); var cacheTag = new CacheTag("a"); string tagKey = MemoryCacheProvider.GetTagKey(cacheTag); tagKey.Should().NotBeNullOrEmpty(); var cachedTag = cacheManager.Get(tagKey); cachedTag.Should().NotBeNull(); // expire actually just changes the value for tag key cacheManager.Expire(cacheTag); var expiredTag = cacheManager.Get(tagKey); expiredTag.Should().NotBeNull(); expiredTag.Should().NotBe(cachedTag); // items should have been removed var expiredValue = cacheManager.Get(cacheKey.Key); expiredValue.Should().BeNull(); var expiredValue2 = cacheManager.Get(cacheKey2.Key); expiredValue2.Should().BeNull(); var expiredValue3 = cacheManager.Get(cacheKey3.Key); expiredValue3.Should().NotBeNull(); }