// // GET: /Home/ public ActionResult Index() { // Get most popular products var topSellingProducts = MemoryCache.Default["topselling"] as List<Product>; if (topSellingProducts == null) { topSellingProducts = GetTopSellingProducts(4); MemoryCache.Default.Add("topselling", topSellingProducts, new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.UtcNow.AddMinutes(10) }); } var newProducts = MemoryCache.Default["newarrivals"] as List<Product>; if (newProducts == null) { newProducts = GetNewProducts(4); MemoryCache.Default.Add("newarrivals", newProducts, new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.UtcNow.AddMinutes(10) }); } var viewModel = new HomeViewModel { NewProducts = newProducts, TopSellingProducts = topSellingProducts, CommunityPosts = GetCommunityPosts() }; return View(viewModel); }
// // GET: /Home/ public IActionResult Index() { // Get most popular products List<Product> topSellingProducts; if (!_cache.TryGetValue("topselling", out topSellingProducts)) { topSellingProducts = GetTopSellingProducts(4); //Refresh it every 10 minutes. Let this be the last item to be removed by cache if cache GC kicks in. _cache.Set("topselling", topSellingProducts, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(10)).SetPriority(CacheItemPriority.High)); } List<Product> newProducts; if (!_cache.TryGetValue("newarrivals", out newProducts)) { newProducts = GetNewProducts(4); //Refresh it every 10 minutes. Let this be the last item to be removed by cache if cache GC kicks in. _cache.Set("newarrivals", newProducts, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(10)).SetPriority(CacheItemPriority.High)); } var viewModel = new HomeViewModel { NewProducts = newProducts, TopSellingProducts = topSellingProducts, CommunityPosts = GetCommunityPosts() }; return View(viewModel); }
// // GET: /Home/ public IActionResult Index() { // Get most popular products var topSellingProducts = _cache.GetOrSet("topselling", context => { //Refresh it every 10 minutes. Let this be the last item to be removed by cache if cache GC kicks in. context.SetAbsoluteExpiration(TimeSpan.FromMinutes(10)); context.SetPriority(CachePreservationPriority.High); return GetTopSellingProducts(4); }); var newProducts = _cache.GetOrSet("newarrivals", context => { context.SetAbsoluteExpiration(TimeSpan.FromMinutes(10)); context.SetPriority(CachePreservationPriority.High); return GetNewProducts(4); }); var viewModel = new HomeViewModel { NewProducts = newProducts, TopSellingProducts = topSellingProducts, CommunityPosts = GetCommunityPosts() }; return View(viewModel); }