Asp. NET: Page Caching

Source: Internet
Author: User
Tags versions

ASP.net provides three main forms of caching: page caching, user control output caching, and caching APIs.

The page output cache, as the simplest form of caching, saves the entire contents of the generated dynamic/Gingtai page in the server's content. When there is another request, the system outputs the relevant data in the cache directly until the cached data expires. In this process, the cache no longer has to go through the page processing lifecycle again. This reduces request response time and improves application performance. Obviously, the page output cache is suitable for pages that do not need to update data frequently, and that consume a lot of time and resources to compile a build.

To implement page output caching, you can usually use the following two methods:

First, use @ OutputCache instruction

With the @ OutputCache directive, you can implement a general need for page output caching. The @ OutputCache directive declares the head of a user control contained in a ASP.net page or page. This approach is very convenient, with just a few simple property settings, you can implement the page's output caching strategy.

Second, the use of HttpCacheability class

This class consists primarily of methods for setting cache-specific HTTP headers and methods for controlling asp.net page output caching. Compared to the HttpCachePolicy classes in the. NET Framework 1.x, the HttpCachePolicy classes in the. NET Framework 2.0 have been expanded and developed.

Using both of these methods, we can implement the following features:

1, use the parameter to cache each version of the page

With asp.net, you can cache multiple versions of a page based on the value of the specified HTTP header. When a page is requested, you can specify that a single header, multiple headers, or all headers that are passed to the application are cached.

To cache versions of a page declaratively based on HTTP header values

A, in the ASP.net page, include the required Duration and VaryByParam or VaryByControl attributes in the @ OutputCache directive. The Duration property must be set to an integer greater than 0. If you want to cache only the HTTP header value, you must set the VaryByParam property to None.

B, in the @ OutputCache directive, include the VaryByHeader property, setting its value to the name of the HTTP header to be used as the basis for changing the cached content.

The following example caches the page for 60 seconds and sets the version of the page to be cached based on the value passed with the Accept-language HTTP header:

<%@ OutputCache duration= varybyparam= "None" varybyheader= "Accept-language"%>

Note: If you want to change the cached content based on multiple headers, use a semicolon (;) Include a list of header names as delimiters. If you want to change the cached content based on all the header values, set the VaryByHeader property to an asterisk (*).

To cache versions of a page programmatically, based on HTTP header values

A, in the page's Page_Load method, calls the SetCacheability and SetExpires methods on the Cache property of the page's Response object.

B, set the HTTP header value in the VaryByHeaders property to True.

The following code example shows how to cache multiple versions of a page for one minute for requests that have different accept-language HTTP header values.

  protected void Page_Load(object sender, EventArgs e)
  {
  Response.Cache.SetExpires(DateTime.Now.AddMinutes(1d));
  Response.Cache.SetCacheability(HttpCacheability.Public);
  Response.Cache.SetValidUntilExpires(true);
  Response.Cache.VaryByHeaders["Accept-Language"] = true;
  }

Note: If you want to change the contents of the cache based on multiple headers, you need to set multiple values in the VaryByHeaders property. If you want to change the contents of the cache based on all headers, set varybyheaders["Varybyunspecifiedparameters" to true.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.