示例#1
0
        public TR Put <T, TR>(string key, T value, TimeSpan timeout, string region) where TR : class
        {
            var abs = new AbsoluteCacheItemPolicyFactory(timeout.Seconds);

            cache.Add(key, value, abs.CreatePolicy());
            return(value as TR);
        }
示例#2
0
        public void ShouldCreateAbsoluteCacheItemPolicyBasedOnTheConfiguration()
        {
            const string PolicyKey = "MDM.Market";

            var appSettings = new NameValueCollection();

            appSettings["CacheItemPolicy.Expiration." + PolicyKey] = "8";

            var configManager = new Mock <IConfigurationManager>();

            configManager.Setup(x => x.AppSettings).Returns(appSettings);

            ICacheItemPolicyFactory policyFactory = new AbsoluteCacheItemPolicyFactory(PolicyKey, configManager.Object);
            var policyItem = policyFactory.CreatePolicy();

            var marketName = "ABC market";
            var marketKey  = "Market-1";
            var cache      = new MemoryCache("MDM.Market");

            cache.Add(marketKey, marketName, policyItem);

            // Should get cache item
            Assert.AreEqual(marketName, cache[marketKey]);

            // wait until the expiry time
            Thread.Sleep(TimeSpan.FromSeconds(10));

            // should not be in the cache
            Assert.IsNull(cache[marketKey]);
        }
        public void ShouldRetrieveFromCacheIfClientRequestBySystemMapping()
        {
            const string CacheKey = "MDM.SourceSystem";

            // Given
            var mockMdmEntityService = new Mock <IMdmEntityService <SourceSystem> >();
            var mockConfigManager    = new Mock <IConfigurationManager>();
            var inmemoryCacheRepo    = new DefaultMdmClientCacheRepository(new InMemoryCacheRepository());

            mockConfigManager.Setup(x => x.AppSettings).Returns(new NameValueCollection {
                { "CacheItemPolicy.Expiration." + CacheKey, "3500" }
            });

            var cachePolicyFactory    = new AbsoluteCacheItemPolicyFactory(CacheKey, mockConfigManager.Object);
            var locationEntityService = new CachePolicyMdmEntityService <SourceSystem>(mockMdmEntityService.Object, cachePolicyFactory, inmemoryCacheRepo);
            var nexusId = new MdmId {
                SystemName = "Nexus", Identifier = "1", IsMdmId = true
            };
            var adcId = new MdmId {
                SystemName = "ADC", Identifier = "123", IsMdmId = false
            };
            var location = new SourceSystem
            {
                Identifiers = new MdmIdList {
                    nexusId, adcId
                },
                Details = new SourceSystemDetails {
                    Name = "Blah"
                }
            };

            //Setup a context, i.e. calling once to retrieve location should cache an entity..
            mockMdmEntityService.Setup(x => x.Get(adcId, It.IsAny <DateTime?>())).Returns(
                new WebResponse <SourceSystem> {
                Code = HttpStatusCode.OK, IsValid = true, Message = location
            });

            var response = locationEntityService.Get(adcId);

            response.Code.Should().Be(HttpStatusCode.OK);
            response.Message.ToMdmId().Identifier.Should().Be(nexusId.Identifier);

            // When , we call second time..
            response = locationEntityService.Get(adcId);

            // Then
            // It should not call mdm service again... should retrive form cache, so that verify...
            mockMdmEntityService.Verify(x => x.Get(adcId, It.IsAny <DateTime?>()), Times.Once());
            mockMdmEntityService.Verify(x => x.Get(It.IsAny <int>(), It.IsAny <DateTime?>()), Times.Never());
            response.Code.Should().Be(HttpStatusCode.OK);
            response.Message.ToMdmId().Identifier.Should().Be(nexusId.Identifier);
        }
示例#4
0
        public void ShouldExpireCacheAfterConfigurableTime()
        {
            const string PolicyKey = "MDM.Market";

            var appSettings = new NameValueCollection();

            appSettings["CacheItemPolicy.Expiration." + PolicyKey] = "8";

            var configManager = new Mock <IConfigurationManager>();

            configManager.Setup(x => x.AppSettings).Returns(appSettings);

            ICacheItemPolicyFactory policyFactory = new AbsoluteCacheItemPolicyFactory(PolicyKey, configManager.Object);
            var policyItem = policyFactory.CreatePolicy();

            var marketName = "ABC market";
            var marketKey  = "Market-1";
            var cache      = new MemoryCache("MDM.Market");

            cache.Add(marketKey, marketName, policyItem);

            // Should get cache item
            Assert.AreEqual(marketName, cache[marketKey]);

            // Keep on accessing cache, it should expire approximately with in 10 iterations
            int count = 0;

            while (cache[marketKey] != null && count < 10)
            {
                count++;
                Thread.Sleep(TimeSpan.FromSeconds(1));
            }

            Console.WriteLine("Cache has expired in {0} seconds:", count);
            // should not be in the cache after configuratble time
            Assert.IsNull(cache[marketKey]);
        }