Compartir a través de


Caching ASP.NET Pages

ASP.NET allows you to cache the entire response content for dynamic pages on HTTP 1.1 capable mechanisms, including browsers, proxy servers, and the Web server where your application resides. This provides a powerful way for you to increase the performance of your Web applications. Called output caching, it allows subsequent requests for a particular page to be satisfied from the cache so the code that initially creates the page does not have to be run upon subsequent requests. Using this technique to cache your site's most frequently accessed pages can increase your Web server's throughput, commonly measured in requests per second, substantially.

You have your choice of a high-level declarative API or a low-level programmatic API when manipulating the output cache for a page. You can use the former by including the @ OutputCache directive in the .aspx file for the page. The @ OutputCache directive can meet nearly all the common needs you may have when you want to cache a page's output. The following directive, when included in an .aspx file, sets an expiration of 60 seconds for the cached output of a dynamically generated page.

<%@ OutputCache Duration="60" VaryByParam="None" %>

CAUTION   When you use the @ OutputCache directive, the Duration and VaryByParam attributes are required. If you do not include them, a parser error occurs when the page is first requested. If you do not want to use the functionality that the VaryByParam attribute provides, you must set its value to None. For more information about using the VaryByParam attribute, see Caching Multiple Versions of a Page.

ASP.NET also includes a set of APIs that control output-cache expirations and policies for a page programmatically through the HttpCachePolicy class. This class, its methods, and its properties are available through the HttpResponse.Cache property. In turn, you can access this property from the Page object through the Page.Response property.

For example, the following code, when included in a page's code-declaration block or its code-behind class, sets an expiration of 60 seconds using the HttpCachePolicy.SetExpires method for the dynamically generated page.

Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
[Visual Basic]
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60))

Once you have enabled output caching, the initial HTTP GET request for the page places its dynamic content in the output cache for the amount of time you specify. The output cache satisfies subsequent GET, HEAD, or POST requests for that page until the amount of time you specify expires.

You can enable or disable page output caching for cache-capable devices in the request stream either declaratively or programmatically as well. In the @ OutputCache directive for a page you can use the Location attribute to specify whether the page's output can be cached on proxy servers, browser clients, the originating Web server, or all or none of these. You can do the same programmatically using the HttpCachePolicy.SetCacheability method to specify the appropriate HttpCacheability enumeration value for your page. For more information, see Setting the Cacheability of a Page.

Responses generated by GET requests with query string parameters or form POST requests with parameters can also be cached, but caching for the passed parameters must be explicitly enabled using the @ OutputCache directive's VaryByParam attribute. For more information, see Caching Multiple Versions of a Page.

Remember that any manipulations that you want to make programmatically to the output cache must be made in the code-declaration block of an .aspx file, or in a code-behind class associated with the .aspx file.

See Also

Setting Expirations for Page Caching | Setting the Cacheability of a Page | Caching Multiple Versions of a Page | ASP.NET Caching Features | @ OutputCache Directive