コード例 #1
0
        public virtual ICacheEntry CreateEntry(object key)
        {
            var result = _memoryCache.CreateEntry(key);

            if (result != null)
            {
                result.RegisterPostEvictionCallback(callback: EvictionCallback);
                var options = GetDefaultCacheEntryOptions();
                //Add GlobalCache token for each entry
                options.AddExpirationToken(GlobalCacheRegion.CreateChangeToken());
                result.SetOptions(options);
            }
            return(result);
        }
コード例 #2
0
        public MemoryCacheEntryOptions GetDefaultCacheEntryOptions()
        {
            var result = new MemoryCacheEntryOptions();

            if (!CacheEnabled)
            {
                result.AbsoluteExpirationRelativeToNow = TimeSpan.FromTicks(1);
            }
            else
            {
                if (AbsoluteExpiration != null)
                {
                    result.AbsoluteExpirationRelativeToNow = AbsoluteExpiration;
                }
                else if (SlidingExpiration != null)
                {
                    result.SlidingExpiration = SlidingExpiration;
                }

                result.AddExpirationToken(GlobalCacheRegion.CreateChangeToken());
            }
            return(result);
        }
コード例 #3
0
        public void CreateChangeTokenReturnsCompositeToken()
        {
            //Act
            var token = CacheRegion.CreateChangeTokenForKey("key");

            //Assertion
            Assert.IsType <CompositeChangeToken>(token);
            //Tokens for item, region and global
            Assert.Equal(3, ((CompositeChangeToken)token).ChangeTokens.Count);
            Assert.All(((CompositeChangeToken)token).ChangeTokens, x => Assert.IsType <LazyCancellationChangeToken>(x));

            //Act
            token = CacheRegion.CreateChangeToken();
            //Assertion
            Assert.IsType <CompositeChangeToken>(token);
            //Tokens for item, region and global
            Assert.Equal(2, ((CompositeChangeToken)token).ChangeTokens.Count);
            Assert.All(((CompositeChangeToken)token).ChangeTokens, x => Assert.IsType <LazyCancellationChangeToken>(x));

            //Act
            token = GlobalCacheRegion.CreateChangeToken();
            //Assertion
            Assert.IsType <LazyCancellationChangeToken>(token);
        }