public override void OnActionExecuted(ActionExecutedContext filterContext) { int shortCacheTime = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["shortCacheTime"]); int StoreID = CommonConfig.StoreID; string TokenKey = CommonConfig.TokenKey; WebService.ProductItem[] saleProduct; WebService.SearchResult bestSaleProduct; WebService.SearchArticleResults topArticle; WebService.Webconfig webConfig; WebService.AnperoService sv = new WebService.AnperoService(); ICacheService cache = new CacheService(); if (!cache.TryGet(BaseController.SaleProductCache, out saleProduct)) { saleProduct = sv.GetSaleProduct(StoreID, TokenKey); cache.AddOrUpdate(BaseController.SaleProductCache, saleProduct, DateTime.Now.AddMinutes(shortCacheTime)); } if (!cache.TryGet(BaseController.BestSaleProductCache, out bestSaleProduct)) { bestSaleProduct = sv.SearchProductFullData(StoreID, TokenKey, "0", "0", "0", 0, 99999999, 1, 7, "", SearchOrder.TimeDesc, 1, true); cache.AddOrUpdate(BaseController.BestSaleProductCache, bestSaleProduct, DateTime.Now.AddMinutes(shortCacheTime)); } if (!cache.TryGet(BaseController.TopArticleCache, out topArticle)) { topArticle = sv.SearchArticle(StoreID, TokenKey, 0, 1, 4, 2); cache.AddOrUpdate(BaseController.TopArticleCache, topArticle, DateTime.Now.AddMinutes(shortCacheTime)); } if (!cache.TryGet(BaseController.CommonInfoCache, out webConfig)) { webConfig = sv.GetCommonConfig(CommonConfig.StoreID, CommonConfig.TokenKey); cache.AddOrUpdate(BaseController.CommonInfoCache, webConfig, DateTime.Now.AddMinutes(shortCacheTime)); } filterContext.Controller.ViewData["saleProduct"] = saleProduct; filterContext.Controller.ViewData["BestsaleProduct"] = bestSaleProduct; filterContext.Controller.ViewData["FeatureArticle"] = topArticle; filterContext.Controller.ViewData["commonInfo"] = webConfig; }
private void SetUpSlideAds() { int shortCacheTime = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["shortCacheTime"]); WebService.AnperoService service = new WebService.AnperoService(); WebService.Ads[] Slide; WebService.Ads[] ads1; WebService.Ads[] ads2; WebService.Ads[] ads3; ICacheService cache = new CacheService(); if (!cache.TryGet(SlideCache, out Slide)) { Slide = service.GetAdsSlide(StoreID, TokenKey, PageContent.Slide); cache.AddOrUpdate(SlideCache, Slide, DateTime.Now.AddMinutes(shortCacheTime)); } if (!cache.TryGet(Ads1Cache, out ads1)) { ads1 = service.GetAdsSlide(StoreID, TokenKey, PageContent.Ads1); cache.AddOrUpdate(Ads1Cache, ads1, DateTime.Now.AddMinutes(shortCacheTime)); } if (!cache.TryGet(Ads2Cache, out ads2)) { ads2 = service.GetAdsSlide(StoreID, TokenKey, PageContent.Ads2); cache.AddOrUpdate(Ads2Cache, ads2, DateTime.Now.AddMinutes(shortCacheTime)); } if (!cache.TryGet(Ads3Cache, out ads3)) { ads3 = service.GetAdsSlide(StoreID, TokenKey, PageContent.Ads3); cache.AddOrUpdate(Ads3Cache, ads3, DateTime.Now.AddMinutes(shortCacheTime)); } ViewBag.Slide = Slide; ViewBag.Ads1 = ads1; ViewBag.Ads2 = ads2; ViewBag.Ads3 = ads3; Response.Cache.SetCacheability(HttpCacheability.Public); }
public void AddOrUpdate_WhenCalledTwoTimesForTheSameKey_OverwritesValueInCache() { var currentTime = new DateTime(2000, 1, 1); var dateTimeProvider = new SettableDateTimeProvider(currentTime); var cacheService = new CacheService(dateTimeProvider); var tokenResponse = CreateTokenResponse(); var cacheKey = "cacheKey"; cacheService.AddOrUpdate(cacheKey, tokenResponse); var newTokenResponse = CreateTokenResponse(); cacheService.AddOrUpdate(cacheKey, newTokenResponse); var cachedValue = cacheService.Get(cacheKey); Assert.AreSame(cachedValue, newTokenResponse); }
public void AddOrUpdate_WhenCalledAndKeyIsNotInCache_AddsValueInCache() { var currentTime = new DateTime(2000, 1, 1); var dateTimeProvider = new SettableDateTimeProvider(currentTime); var cacheService = new CacheService(dateTimeProvider); var tokenResponse = CreateTokenResponse(); var cacheKey = "cacheKey"; cacheService.AddOrUpdate(cacheKey, tokenResponse); var cachedValue = cacheService.Get(cacheKey); Assert.AreSame(cachedValue, tokenResponse); }
private void GetNewestProduct() { WebService.AnperoService service = new WebService.AnperoService(); WebService.SearchResult searchResult = new WebService.SearchResult(); int shortCacheTime = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["shortCacheTime"]); ICacheService cache = new CacheService(); if (!cache.TryGet(NewestProductCache, out searchResult)) { searchResult = service.SearchProduct(StoreID, TokenKey, "", "", "", 0, 999999999, 1, 19, "", SearchOrder.TimeDesc, 0, string.Empty); cache.AddOrUpdate(NewestProductCache, searchResult, DateTime.Now.AddMinutes(shortCacheTime)); } ViewData["newestProduct"] = searchResult; }
public void Get_WhenCalledForACachedValue_ReturnsBasedOnInvalidCacheWindow(int windowInSeconds, bool isValid) { var currentTime = new DateTime(2000, 1, 1); var dateTimeProvider = new SettableDateTimeProvider(currentTime); var cacheService = new CacheService(dateTimeProvider); var tokenResponse = CreateTokenResponse(); var cacheKey = "cacheKey"; cacheService.AddOrUpdate(cacheKey, tokenResponse); dateTimeProvider.Now = currentTime.AddSeconds(tokenResponse.ExpiresIn).Subtract(TimeSpan.FromSeconds(windowInSeconds)); var cachedValue = cacheService.Get(cacheKey); var expectedIsValid = cachedValue != null; Assert.AreEqual(expectedIsValid, isValid); }
public void Get_WhenCacheIsExpired_ReturnsNull() { var currentTime = new DateTime(2000, 1, 1); var dateTimeProvider = new SettableDateTimeProvider(currentTime); var cacheService = new CacheService(dateTimeProvider); var tokenResponse = CreateTokenResponse(); var cacheKey = "cacheKey"; cacheService.AddOrUpdate(cacheKey, tokenResponse); var newCurrentTime = currentTime.AddMinutes(20); dateTimeProvider.Now = newCurrentTime; var response = cacheService.Get(cacheKey); Assert.AreEqual(null, response); }
public void Get_WhenCacheIsNotExpired_ReturnsCachedValue() { var currentTime = new DateTime(2000, 1, 1); var datetimeProviderStub = new SettableDateTimeProvider(currentTime); var tokenResponse = CreateTokenResponse(); var cacheService = new CacheService(datetimeProviderStub); var cacheKey = "cacheKey"; cacheService.AddOrUpdate(cacheKey, tokenResponse); datetimeProviderStub.Now = currentTime.AddMinutes(10); var response = cacheService.Get(cacheKey); Assert.AreEqual("access_token", response.AccessToken); Assert.AreEqual("token_type", response.TokenType); Assert.AreEqual(1000, response.ExpiresIn); Assert.AreEqual("https://rest.com", response.RestInstanceUrl); Assert.AreEqual("https://soap.com", response.SoapInstanceUrl); }
private void SetUpSeo(int type, int categoryId) { AnperoFrontend.WebService.Webconfig commonInfo; ICacheService cache = new CacheService(); if (!cache.TryGet(CommonInfoCache, out commonInfo)) { var service = new WebService.AnperoService(); commonInfo = service.GetCommonConfig(CommonConfig.StoreID, CommonConfig.TokenKey); cache.AddOrUpdate(CommonInfoCache, commonInfo, DateTime.Now.AddMinutes(shortCacheTime)); } ViewData["commonInfo"] = commonInfo; //Get Description and Keywords of Category production ViewBag.Description = string.Empty; ViewBag.Keywords = string.Empty; ViewBag.WebsiteUrl = string.Empty; ViewBag.ImageUrl = string.Empty; string title = ViewBag.Title; if (commonInfo != null) { if (string.IsNullOrEmpty(title)) { ViewBag.Title = "Tìm kiếm trên " + commonInfo.Name; } switch (type) { case 1: foreach (var item in commonInfo.ProductCategoryList) { if (item.Id == categoryId) { ViewBag.Keywords = item.Keywords; ViewBag.Description = item.Description; ViewBag.WebsiteUrl = Request.Url.Scheme + System.Uri.SchemeDelimiter + Request.Url.Host + Anpero.StringHelpper.GetParentCategoryLink(item.Name, item.Id); ViewBag.ImageUrl = item.Images; break; } } break; case 2: foreach (var item in commonInfo.ProductCategoryList) { foreach (var chidItem in item.ChildCategory) { if (chidItem.Id == categoryId) { ViewBag.Keywords = item.Keywords; ViewBag.Description = item.Description; ViewBag.WebsiteUrl = Request.Url.Scheme + System.Uri.SchemeDelimiter + Request.Url.Host + Anpero.StringHelpper.GetCategoryLink(chidItem.Name, chidItem.Id); ViewBag.ImageUrl = item.Images; break; } } } break; default: ViewBag.Keywords = "Tìm kiếm " + commonInfo.Name + "| " + commonInfo.Desc; ViewBag.Description = "Tìm kiếm trên " + commonInfo.Name + "| " + commonInfo.Desc; ViewBag.WebsiteUrl = Request.Url.AbsoluteUri; ViewBag.ImageUrl = commonInfo.Logo; break; } } }