Page output cache in ASP. NET 2.0

Source: Internet
Author: User

All static page content is stored in the server memory. When there is another request, the system will directly output the relevant data in the cache until the cached data expires. In this process, the cache does not need to go through the page processing lifecycle again. This can shorten the request response time and improve the application Program Performance. Obviously, the page output cache is suitable for pages that take up a lot of time and resources to compile and generate without frequently updating data. It is not applicable to pages with frequently updated data. By default, ASP. NET 2.0 enables the page output caching function, but does not cache any response output. Developers must set the response of some pages to become part of the cache.

You can use either the @ outputcache command or the page output cache API to set the page output cache. @ Outputcache commands have appeared in ASP. NET 1.x and are inherited and enhanced in ASP. NET 2.0. The page output cache API mainly refers to the httpcachepolicy class.

Use the @ outputcache command

Use the @ outputcache command to implement general requirements for page output cache. @ Outputcache command the header Declaration of the user control contained in the ASP. NET page or page. This method is very convenient. You only need a few simple attribute settings to implement the page output Cache Policy. @ Outputcache command DeclarationCodeAs follows.

@ Outputcache command code

<% @ OutputcacheCacheprofile= ""Nostore ="True | false"Duration= "# Ofseconds"Shared= "True | false"Location= "Any | client | downstream | Server | none | serverandclient"Sqldependency= "Database/table name pair | commandnotification"Varybycontrol= "Controlname"Varybycustom= "Browser | customstring"Varybyheader= "Headers"Varybyparam= "Parametername" %>

As shown above, the @ outputcache command contains 10 attributes, including cacheprofile, nostore, duration, shared, location, sqldependency, varybycontrol, varybycustom, varybyheader, and varybyparam. These attributes are used to set the cache time, cache item location, and SQL data cache dependency. The following describes the basic concepts of the above attributes.

Cacheprofile

Defines the name of the cache settings associated with this page. Is an optional attribute. The default value is null (""). Note that the @ outputcache command in the user control does not support this attribute. When this attribute is specified on the page, the attribute value must match the name of an available item in the outputcacheprofiles element in the <outputcachesetfiles> Configuration section of the web. config file. If the name does not match the configuration file, an exception is thrown.

Nostore

This attribute defines a Boolean value to determine whether to block secondary storage of sensitive information. Note that the @ outputcache command in the user control does not support this attribute. Setting this attribute to true is equivalent to executing the code "response. cache. setnostore ();" during the request ();".

Duration

Used to set the cache time for pages or user controls. The Unit is seconds. By setting this attribute, you can create an expiration Policy for the HTTP response from the object and automatically cache the page or user control output. Note that the duration attribute is required. Otherwise, the analyzer error may occur.

Shared

This attribute defines a Boolean value to determine whether the output of a user control can be shared by multiple pages. The default value is false. Note that the @ outputcache Command included in the ASP. NET page does not support this attribute.

Location

Specifies the location of the output cache. Its Attribute values are outputcachelocation enumeration values, which are any, client, downstream, none, server, and serverandclient. The default value is any, indicating that the output cache can be used for all requests, including the client browser, proxy server, or server that processes the request. Note that the @ outputcache command in the user control does not support this attribute.

Sqldependency

This attribute identifies the string value of a group of database/table name pairs. The output cache of pages or controls depends on these name pairs. Note: The sqlcachedependency class monitors the tables in the database on which the output cache depends. Therefore, when items in the table are updated, table-based round robin is used to remove these items from the cache. When the notification (in SQL Server 2005) is used with the commandnotification value, the sqldependency class is used to register the query notification with the SQL Server 2005 server. In addition, the commandnotification value of the sqldependency attribute is only valid on the ASP. NET page. The control can only use table-based round robin For the @ outputcache command.

Varybycontrol

This attribute uses a semicolon-separated string list to change the output cache of the user control. These strings represent the ID attribute values of the ASP. NET Server Control declared in the user control. This attribute is required in the @ outputcache command unless the varybyparam attribute is already included.

Varybycustom

It is used to customize any text required by the output cache. If the property value is browser, the cache varies with the browser name and major version information. If you enter a custom string, you must re-write httpapplication. getvarybycustomstring in the global. asax file of the application.

Varybyheader

This attribute contains the HTTP header list separated by semicolons to change the output cache. When this attribute is set to multiple headers, the output cache contains different versions of a request document for each specified header. The varybyheader attribute enables cache items in all HTTP 1.1 caches, not limited to ASP. NET caches. The @ outputcache command in the user control does not support this attribute.

Varybyparam

This attribute defines a semicolon-separated string list to change the output cache. By default, these strings correspond to the query string values sent using the get method attribute, or to parameters sent using the POST method. When this attribute is set to multiple parameters, the output cache contains different versions of the Request Document for each specified parameter. Possible values include "NONE", "*", and any valid query string or post parameter names. It is worth noting that this attribute is required when the ASP. NET page is cached. It is also required for user controls, unless the varybycontrol attribute is included in the @ outputcache command of the user control. If not, a analyzer error occurs. If you do not need to change the cache content with any specified parameter, you can set this value to "NONE ". If you want the output cache to change according to all parameter values, set the attribute to "*".

The following lists two sample codes using the @ outputcache command.

Sample Code 1 using @ outputcache

<% @ Outputcache duration = "100" varybyparam = "NONE" %>

The preceding example shows the basic application of the @ outputcache command. It indicates that the validity period of the page output cache is 100 seconds, and the page does not change with any get or post parameters. The request received when the page is still cached is provided by the cached data. After 100 seconds, the page data will be removed from the cache, And the next request will be processed explicitly and the page will be cached again.

Sample Code 2 using @ outputcache


<% @ Outputcache duration = "100" varybyparam = "location; firstname" %>

The above @ outputcache command sets the page output cache to be valid for 100 seconds, and sets the output cache based on the query string parameter location or firstname. For example, assume that the client request is "http: // localhost/default. aspx? Location = Beijing ", the page will be processed as a cache.

All static page content is stored in the server memory. When there is another request, the system will directly output the relevant data in the cache until the cached data expires. In this process, the cache does not need to go through the page processing lifecycle again. This can shorten the request response time and improve application performance. Obviously, the page output cache is suitable for pages that take up a lot of time and resources to compile and generate without frequently updating data. It is not applicable to pages with frequently updated data. By default, ASP. NET 2.0 enables the page output caching function, but does not cache any response output. Developers must set the response of some pages to become part of the cache.

You can use either the @ outputcache command or the page output cache API to set the page output cache. @ Outputcache commands have appeared in ASP. NET 1.x and are inherited and enhanced in ASP. NET 2.0. The page output cache API mainly refers to the httpcachepolicy class.

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.