www.gibmonks.com

Main Page

  Previous Section Next Section

HttpCachePolicy

System.Web (system.web.dll)sealed class

This class allows you to configure "page" or output caching for an ASP.NET application, which stores a fully rendered page for automatic reuse. A cached page will be used for GET requests until it expires, as long as the URL request does not have different query string arguments. A cached page will not be used for POST requests, so postbacks (such as when a user clicks on button) will bypass the cached page. (This behavior is slightly and mysteriously different than just using the <OutputCache> page directive, which always reuses the cached page for any type of request when the VaryByParam attribute is set to None.) To specifically modify this behavior, you can use the VaryByParams property and the HttpCacheVaryByParams class.

To enable caching for a page, use the SetCacheability( ) method to set the page's visibility to Public so it can be stored in the shared cache. Then use the SetExpires( ) method to determine the lifetime of the page in the cache. For example, Response.Cache.SetExpires(DateTime.Now.AddSeconds(60)); will keep a page for 60 seconds, which is enough to make a substantial performance difference. By default, the cache uses absolute expiration. You can also invoke the SetSlidingExpiration( ) method, with the parameter True, to enable sliding expiration. In sliding expiration, the time limit is compared to the time elapsed since the most recent request, not the time since the first request. You can also use the AddValidationCallback( ) method to add a callback that decides on a page-by-page basis whether to allow a cached page to be served. Finally, you can use fragment caching by developing a Web Form user control for a portion of a page, and caching just that portion using page directives or the methods of this class.

If your page requires customization based on Session variables or user-specific details other than a query string, you shouldn't cache the page! In these cases, data caching will be more useful. With data caching, you manually store specific information, such as binary data or recordsets. For more information on data caching, refer to the System.Web.Caching.Cache class.

The HttpCachePolicy class is available through the Cache property of the built-in Response object. It replaces properties of the Response object that were used to configure caching in ASP (like CacheControl and Expires). Microsoft uses somewhat confusing nomenclature. An instance of the HttpCachePolicy class (used to configure page caching) is available through the built-in Response.Cache object, and the System.Web.Caching.Cache class (used for data caching) is available through the built-in Cache object. Cache and Response.Cache are not the same!

public sealed class HttpCachePolicy {
// Public Instance Properties
   public HttpCacheVaryByHeaders VaryByHeaders{get; }
   public HttpCacheVaryByParams VaryByParams{get; }
// Public Instance Methods
   public void AddValidationCallback(HttpCacheValidateHandler handler, object data);
   public void AppendCacheExtension(string extension);
   public void SetAllowResponseInBrowserHistory(bool allow);
   public void SetCacheability(HttpCacheability cacheability);
   public void SetCacheability(HttpCacheability cacheability, string field);
   public void SetETag(string etag);
   public void SetETagFromFileDependencies( );
   public void SetExpires(DateTime date);
   public void SetLastModified(DateTime date);
   public void SetLastModifiedFromFileDependencies( );
   public void SetMaxAge(TimeSpan delta);
   public void SetNoServerCaching( );
   public void SetNoStore( );
   public void SetNoTransforms( );
   public void SetProxyMaxAge(TimeSpan delta);
   public void SetRevalidation(HttpCacheRevalidation revalidation);
   public void SetSlidingExpiration(bool slide);
   public void SetValidUntilExpires(bool validUntilExpires);
   public void SetVaryByCustom(string custom);
}

Returned By

HttpResponse.Cache

      Previous Section Next Section