The page output cache is the simplest form of cache. The output cache only keeps the HTML copies sent in response to the request in the memory. Later, when there is another request, the cache output will be provided until the cache expires. In this way, the performance may be greatly improved.
You can cache the page output in the following ways:
Specify the cache settings in the form of Declaration on the page or configuration file or by using the cache API programmatically.
The cache page is queried Based on the string parameter value or form variable value (control value. Must be used Varybyparam Attribute to explicitly enable caching based on these types of values.
The following describes the implementation method.
1. Set page cacheability in declarative Mode
Use the @ outputcache command to declare the header of the user control on the ASP. NET page or page. Example Code As follows:
<% @ Outputcache duration = "# ofseconds"
Location = "any | client | downstream | Server | none | serverandclient"
Shared = "True | false"
Varybycontrol = "controlname"
Varybycustom = "browser | customstring"
Varybyheader = "headers"
Varybyparam = "parametername"
Cacheprofile = "cache profile name | ''"
Nostore = "True | false"
Sqldependency = "database/table name pair | commandnotification"
%>
Attribute
Duration: the cache time for pages or user controls (in seconds ). Note: Required attribute . If this attribute is not included, a analyzer error occurs.
Location: Specifies the location of the output cache entry,Outputcachelocation One of the enumerated values. The default value is any. Note: The @ outputcache instruction in the user control (. ascx file) does not support this attribute.
Cacheprofile: name of the cache settings associated with this page. This is an optional attribute. The default value is null (""). Note:The @ outputcache directive contained in the user control (. ascx file) 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: A boolean value that determines whether to block the second-level storage of sensitive information. Note: The @ outputcache instruction in the user control (. ascx file) does not support this attribute. Setting this attribute to true is equivalent to executing the Code during the request: Response. cache. setnostore ();
Shared: A boolean value to determine whether the output of a user control can be shared by multiple pages. The default value is false. Note: The @ outputcache instruction in the ASP. NET page (. aspx file) does not support this attribute.
Sqldependency: the string value that identifies a group of database/table name pairs. The output cache of pages or controls depends on these name pairs. Note: S Qlcachedepende Ncy monitors the tables in the database on which the output cache depends. Therefore, when items in the table are updated, these items will be removed from the cache when table-based round robin is used. When a notification is used with the commandnotification value (in Microsoft SQL Server 2005), The sqldependency class is used to register the query notification with the SQL Server 2005 server. Note: The commandnotification value of the sqldependency attribute is only valid on the webpage (. aspx. The user control can only use table-based round robin For the @ outputcache command.
Varybycustom: Indicates any text required by the custom output cache. If the value of this attribute is browser, the cache will vary with the browser name and major version information. If you enter a custom stringProgramIn the global. asax file Getvarybycustomstring method.
varybyheader: a semicolon-separated HTTP header list used to change the output cache. When this attribute is set to multiple headers, the output cache contains a different version of the Request Document for each specified header combination. Note: Setting the varybyheader attribute will enable cache items in all HTTP 1.1 caches, not just in ASP. NET caches. The @ outputcache command in the user control does not support this attribute.
varybyparam: a semicolon-separated string list used to change the output cache. By default, these strings correspond to the query string values sent with the get method attribute, or to parameters sent using the POST method. When this attribute is set to multiple parameters, the output cache contains a different version of the Request Document for each specified parameter combination. Possible values include none, asterisks (*), and any valid query string or post parameter names. Note: This attribute or varybycontrol is required when @ outputcache command is used on ASP. NET pages and user controls. If it is not included, an analyzer error occurs. If you do not want to change the cache content by specifying parameters, set the value to none. If you want to change the output cache by using all parameter values, set the attribute to asterisk (*)).
varybycontrol: a semicolon-separated string list used to modify 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. Note: This attribute or varybyparam attribute is required when @ outputcache command is used on ASP. NET pages and user controls.
a simple example of using the web. config file to declare the page cache:
1. Set web. config
Sample Code
< Caching >
< Outputcachesettings >
< Outputcacheprofiles >
< Add Name = "Cache30seconds" Duration = "30"
Varybyparam = "NONE" />
</ Outputcacheprofiles >
</ Outputcachesettings >
</ Caching >
2. Use the @ outputcache command on the ASP. NET page
<% @ Outputcache cacheprofile = "cache30seconds" %>
2. Set page cache by programming
The core of this method is to call system. Web. httocachepolicy. The following shows an example and some common methods.
In the Page code, callThe setcacheability method of the cache attribute.
The following code sets the cache-control HTTP header to public.
Response. cache. setcacheability (httpcacheability. Public );
Common httpcachepolicy Methods
Setexpires: sets the expires HTTP header to an absolute date and time.
Setlastmodified: Set the last-modified HTTP header to the provided datetime value.
Setslidingexpiration: sets the cache expiration time from the absolute time to the adjustable time.
Setomitvarystar: Specifies whether the response should contain the vary: * Header when partitioning by parameter.
Setcacheability:Overloaded. Set the cache-control HTTP header. Cache-control the HTTP header control the way documents are cached on the network.