[HttpGet] public ActionResult CartSummary() { // , this.Response.Cache.SetCacheability(System.Web.HttpCacheability.Private); this.Response.Cache.SetMaxAge(TimeSpan.Zero); var cart = ShoppingCart.GetCart(this.HttpContext); var cacheKey = "shooting-cart-" + cart.ShoppingCartId; var cachedPair = (Tuple<DateTime, int>)this.HttpContext.Cache[cacheKey]; if (cachedPair != null) // { // Last-Modified this.Response.Cache.SetLastModified(cachedPair.Item1); var lastModified = DateTime.MinValue; // Conditional Get if (DateTime.TryParse(this.Request.Headers["If-Modified-Since"], out lastModified) && lastModified >= cachedPair.Item1) { return new NotModifiedResult(); } ViewData["CartCount"] = cachedPair.Item2; } else // { // , var now = DateTime.Now; now = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second); // Last-Modified this.Response.Cache.SetLastModified(now); var count = cart.GetCount(); this.HttpContext.Cache[cacheKey] = Tuple.Create(now, count); ViewData["CartCount"] = count; } return PartialView("CartSummary"); } public ActionResult CartSummary() { var cart = ShoppingCart.GetCart(this.HttpContext); ViewData["CartCount"] = cart.GetCount(); return PartialView("CartSummary"); } System.Web.Caching.Cache ). Response.AddCacheItemDependency(cacheKey); Last-Modified header and \ or E-Tag . There are also functions for this: Response.Cache.SetLastModifiedFromFileDependencies(); Response.Cache.SetETagFromFileDependencies(); File in the function name, any dependencies of the response are analyzed. Moreover, if the server response has many dependencies, then Last-Modified set to the highest value, and the E-Tag is formed from all dependencies. Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate); Last-Modified , E-Tag , Cache-Control: private and stores the response on the server. But there is a problem - IE does not request a new version of the page, caching the default response for a day or until the browser restarts. In general, response caching time without specifying max-age or Expires header can vary greatly between browsers.max-age=0 . In ASP.NET, this can be done with the following function: Response.Cache.SetMaxAge(TimeSpan.FromSeconds(0)); Response.Cache.AppendCacheExtension("max-age=0") Cache-Control: private, max-age=0 header is given to the client Cache-Control: private, max-age=0 , which causes the browser to send a request every time. Unfortunately, this method is not documented anywhere.cacheKey key. [HttpGet] public ActionResult CartSummary() { var cart = ShoppingCart.GetCart(this.HttpContext); var cacheKey = "shopping-cart-" + cart.ShoppingCartId; ViewData["CartCount"] = GetCachedCount(cart, cacheKey); this.Response.AddCacheItemDependency(cacheKey); this.Response.Cache.SetLastModifiedFromFileDependencies(); this.Response.Cache.AppendCacheExtension("max-age=0"); this.Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate); return PartialView("CartSummary"); } private int GetCachedCount(ShoppingCart cart,string cacheKey) { var value = this.HttpContext.Cache[cacheKey]; int result = 0; if (value != null) { result = (int) value; } else { result = cart.GetCount(); this.HttpContext.Cache.Insert(cacheKey,result); } return result; } Accept-Encoding header), and also by the custom parameter associated with the response. Response.Cache.SetVaryByCustom("sessionId"); Global.asax override the GetVaryByCustomString method public override string GetVaryByCustomString(HttpContext context, string custom) { if (custom == "sessionId") { var sessionCookie = context.Request.Cookies["ASP.NET_SessionId"]; if (sessionCookie != null) { return sessionCookie.Value; } } return base.GetVaryByCustomString(context, custom); } CacheDependency class and its descendants are responsible for this. HttpContext.Cache.Insert("cacheItemKey",data, new CacheDependency(null, new[] { "anotherCacheItemKey" })); anotherCacheItemKey key is changed or deleted from the cache, the item with the cacheItemKey key will be automatically deleted from the cache.Source: https://habr.com/ru/post/227129/
All Articles