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]); }
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 ShouldRetrieveFromCacheIfClientRequestByNexusIdMapping() { 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 location = new SourceSystem { Identifiers = new MdmIdList { nexusId }, Details = new SourceSystemDetails { Name = "Blah" } }; //Setup a context, i.e. calling once to retrieve location should cache an entity.. mockMdmEntityService.Setup(x => x.Get(1, It.IsAny<DateTime?>())).Returns( new WebResponse<SourceSystem> { Code = HttpStatusCode.OK, IsValid = true, Message = location }); var response = locationEntityService.Get(nexusId); response.Code.Should().Be(HttpStatusCode.OK); response.Message.ToMdmId().Identifier.Should().Be(nexusId.Identifier); // When , we call second time.. response = locationEntityService.Get(nexusId); // Then // It should not call mdm service again... should retrive form cache, so that verify... mockMdmEntityService.Verify(x => x.Get(1, It.IsAny<DateTime?>()), Times.Once()); mockMdmEntityService.Verify(x => x.Get(It.IsAny<MdmId>(), It.IsAny<DateTime?>()), Times.Never()); response.Code.Should().Be(HttpStatusCode.OK); response.Message.ToMdmId().Identifier.Should().Be(nexusId.Identifier); }
public static void RegisterAbsoluteCacheItemPolicyFactory(this IUnityContainer container, string key) { var policyFactory = new AbsoluteCacheItemPolicyFactory(key); container.RegisterInstance <ICacheItemPolicyFactory>(key, policyFactory); }
public static void RegisterAbsoluteCacheItemPolicyFactory(this IUnityContainer container, string key) { var policyFactory = new AbsoluteCacheItemPolicyFactory(key); container.RegisterInstance<ICacheItemPolicyFactory>(key, policyFactory); }