コード例 #1
0
        public void Save_ReferenceType_then_get()
        {
            var            key   = "key-object";
            ICacheProvider cache = new MemoryCacheProvider("region2");
            var            id1   = new Object();
            var            id2   = cache.GetOrCreate(key, _ => id1);

            Assert.AreEqual(id1, id2);

            cache.Expire(key);
            Object id3;
            var    exists = cache.TryGet(key, out id3);

            Assert.IsFalse(exists);
            Assert.AreNotEqual(id1, id3);
            Assert.AreEqual(id3, null);
        }
コード例 #2
0
        public void Save_ValueType_then_get()
        {
            var            key   = "key-guid";
            ICacheProvider cache = new MemoryCacheProvider("region1");
            var            id1   = Guid.NewGuid();
            var            id2   = cache.GetOrCreate(key, _ => id1);

            Assert.AreEqual(id1, id2);

            cache.Expire(key);
            Guid id3;
            var  exists = cache.TryGet(key, out id3);

            Assert.IsFalse(exists);
            Assert.AreNotEqual(id1, id3);
            Assert.AreEqual(id3, Guid.Empty);
        }
コード例 #3
0
        public void Set_then_expire()
        {
            var key   = Guid.NewGuid().ToString();
            var value = Guid.NewGuid();

            var cache = new MemoryCacheProvider("region6");

            cache.Overwrite(key, value);

            cache.Expire(key);
            Guid value2;
            var  exist = cache.TryGet(key, out value2);

            Assert.IsFalse(exist);
            Assert.AreEqual(value2, Guid.Empty);

            cache.ExpireAll();
            Assert.AreEqual(cache.Count(), 0);
        }
コード例 #4
0
        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);
        }