public void AddWithExistingTagTest()
        {
            var provider = new MemoryCacheProvider();
            string key = "AddWithExistingTagTest" + 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 = "AddWithExistingTagTest2" + 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 AddWithTagsTest()
        {
            var provider = new MemoryCacheProvider();
            string key = "AddWithTagsTest" + 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 AddTest()
        {
            var provider = new MemoryCacheProvider();
            var cacheKey = new CacheKey("AddTest" + DateTime.Now.Ticks);
            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);
        }
        public void GetOrAddTest()
        {
            var provider = new MemoryCacheProvider();
            var cacheKey = new CacheKey("GetOrAddTest" + DateTime.Now.Ticks);
            var value = "Test Value " + DateTime.Now;
            var cachePolicy = new CachePolicy();
            int callCount = 0;

            Func<CacheKey, object> valueFactory = k =>
            {
                callCount++;
                return value;
            };

            var result = provider.GetOrAdd(cacheKey, valueFactory, cachePolicy);
            result.Should().Be(value);
            callCount.Should().Be(1);

            // look in underlying MemoryCache
            string innerKey = MemoryCacheProvider.GetKey(cacheKey);
            var cachedValue = MemoryCache.Default.Get(innerKey);
            cachedValue.Should().NotBeNull();
            cachedValue.Should().Be(value);

            callCount = 0;
            var result2 = provider.GetOrAdd(cacheKey, valueFactory, cachePolicy);
            result2.Should().Be(value);
            callCount.Should().Be(0);
        }
        public void GetTest()
        {
            var provider = new MemoryCacheProvider();
            var cacheKey = new CacheKey("GetTest" + DateTime.Now.Ticks);
            var value = "Get Value " + DateTime.Now;
            var cachePolicy = new CachePolicy();

            bool result = provider.Add(cacheKey, value, cachePolicy);
            result.Should().BeTrue();

            var existing = provider.Get<string>(cacheKey);
            existing.Should().NotBeNull();
            existing.Should().BeSameAs(value);
        }
        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 = "ExpireTest" + 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 = "ExpireTest2" + 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 = "ExpireTest3" + 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();

            System.Threading.Thread.Sleep(500);

            // 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<string>(cacheKey);
            expiredValue.Should().BeNull();

            var expiredValue2 = provider.Get<string>(cacheKey2);
            expiredValue2.Should().BeNull();

            var expiredValue3 = provider.Get<string>(cacheKey3);
            expiredValue3.Should().NotBeNull();

            cache.GetCount().Should().Be(4);
        }