public async Task WHEN_Passing_Any_Culture_Resulting_TreeStructure_SHOULD_Be_Cached_ByCulture() { //Arrange CacheProviderFactory.CacheHistMonitor monitor = new CacheProviderFactory.CacheHistMonitor(); LocalizationTree cachedTree = new LocalizationTree(CultureInfo.CurrentCulture); AutoMocker container = new AutoMocker(); container.Use(CacheProviderFactory.CreateWithMonitor(monitor, cachedTree)); container.Use(ComposerEnvironmentFactory.Create()); ILocalizationProvider localizationProvider = container.CreateInstance <ResourceLocalizationProvider>(); CultureInfo cultureA = CultureInfo.GetCultureInfo("fr-CA"); CultureInfo cultureB = CultureInfo.GetCultureInfo("fr-FR"); //Act monitor.Reset(); monitor.CacheMissCount.ShouldBeEquivalentTo(0, "Otherwise this test is irrelevent"); monitor.CacheHitCount.ShouldBeEquivalentTo(0, "Otherwise this test is irrelevent"); LocalizationTree tree1A = await localizationProvider.GetLocalizationTreeAsync(cultureA); monitor.CacheMissCount.ShouldBeEquivalentTo(1, "First attempt to load the CultureA should cache miss"); LocalizationTree tree2A = await localizationProvider.GetLocalizationTreeAsync(cultureA); monitor.CacheHitCount.ShouldBeEquivalentTo(1, "Second attempt to load the CultureA should cache hit"); monitor.Reset(); for (int i = 0; i < 10; i++) { LocalizationTree tree3A = await localizationProvider.GetLocalizationTreeAsync(cultureA); } monitor.CacheMissCount.ShouldBeEquivalentTo(0, "Subsequent attempt to load the CultureA should not cache miss"); monitor.CacheHitCount.Should().BeGreaterOrEqualTo(10, "Subsequent attempt to load the CultureA should cache hit"); //-- monitor.Reset(); LocalizationTree tree1B = await localizationProvider.GetLocalizationTreeAsync(cultureB); monitor.CacheMissCount.ShouldBeEquivalentTo(1, "First attempt to load the CultureB should cache miss, key is culture dependant"); monitor.CacheHitCount.ShouldBeEquivalentTo(0, "First attempt to load the CultureB should not cache hit, key is culture dependant"); }
public ActionResult GetTree(string language) { var tree = _localizationProvider.GetLocalizationTreeAsync(_composerContext.CultureInfo).Result; var cache = ComposerConfiguration.LocalizationCacheOptions; //Only this work in Sitecore. OutputCache attribute doesn't work. Response.Cache.SetExpires(DateTime.UtcNow.AddSeconds(cache.Duration)); //Security Note: It is safe to AllowGet only as long as the dto is not an array. return(Json(tree, JsonRequestBehavior.AllowGet)); }