Page output Cache
This cache mechanism is a simple cache mechanism in the. NET environment. It stores the content of the entire ASPX page in the server's memory. In this way, when you access this page, you can directly call it from the server memory, instead of interacting with the database, and retrieve the data from the database again unless the cache data expires. This environment is suitable for some data pages that do not need to be changed frequently.
The usage is very simple. Add the following declaration at the top of the ASPX page.
<% @OutputcacheDuration = "60"Varybyparam = "NONE"%>
For the above declaration, explain the meaning of the parameter:
Duration: cache expiration time (unit: seconds ). The preceding statement indicates that the page expires every 60 seconds (that is, within 60 seconds, the page reads data in the server cache and does not directly obtain data from the database ), this parameter is required!
Varybyparam: Updates cache content based on post (or get) parameters. Separate multiple parameters with semicolons. If you do not want any parameters to modify the cached content, set the value to none. If you want all parameters to modify the cached content, set the value :*.
Example:
Test page cache-front-end code
1 <% @ page Language = "C #" codebehind = "default. aspx. cs" inherits = "testwebcache. Default" %>
2 <% @ outputcache duration = "60" varybyparam = "NONE" %>
3
4 <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
6 <HTML xmlns = "http://www.w3.org/1999/xhtml">
7 8 <title> test page cache </title>
9 10 <body>
11 <Form ID = "form1" runat = "server">
12 <asp: literal id = "literal1" runat = "server"/>
13 </form>
14 </body>
15 2 {
3 if (! Ispostback)
4 {
5 literal1.text = datetime. Now. tostring ();
6}
7}
If you want to update the cache based on a parameter in the URL, you can add the parameter name after the equal sign of "varybyparam". For example, in general, when accessing this page, we can enter the URL: http: // localhost: 17112/default. aspx is used to access this page even with parameters, for example, http: // localhost: 17112/default. aspx? A = 1 does not affect the content of the page, because when the cache is declared at the top of the page, I fill in "NONE" in the value after "varybyparam ", if you change the value to the parameter "A" in the second URL above, the page content will be changed because of the change of the parameter ", when "a" is assigned the same value, the page does not change. If the value of the parameter "a" is changed within the page expiration time (that is, 60 seconds, the page content also changes accordingly.
If you need multiple parameters, such as http: // localhost: 17112/default. aspx? A = 1 & B = 2 & C = 3. When declaring the page cache, you should assign the value of "varybyparam" to a; B; C, that is: varybyparam = "A; B; C ".
Other attribute parameters in the outputcache command are described as follows:
Other outputcache attributes
1<% @
2 OutputcacheDuration = "# ofseconds"
3 Location = "any|Client|Downstream|Server|None|Serverandclient"
4 Shared = "true|False"
5 Varybycontrol = "controlname"
6 Varybycustom = "Browser|Customstring"
7 Varybyheader = "headers"
8 Varybyparam = "parametername"
9 Cacheprofile = "cacheProfileName|''"
10 Nostore = "true|False"
11 Sqldependency = "database/tableNamePair|Commandnotification"
12%>
Of course, if many pages are added with the same cache time, we can use the web. config to configure the cache expiration time in a unified manner, so that one change can be made and play a corresponding role in multiple places. The configuration example is as follows:
Web. config Configuration
1 <system. Web>
2 <caching>
3 <outputcachesettings>
4 <outputcacheprofiles>
5 <Add name = "cacheconfig" Duration = "60"/>
6 </outputcacheprofiles>
7 </outputcachesettings>
8 </caching>
9 </system. Web>
In this way, we can add the following page cache declaration method on the top of the page:
1<% @OutputcacheCacheprofile = "cacheconfig"Varybyparam = "NONE"%>
However, pay attention to the following points:
1. If outputcache is declared in the user control on the page, the cacheprofile attribute is not supported.
2. The added cacheprofile must exist in Web. config.
You can also modify the page cache by changing the controls on the page. The declaration is as follows (assuming there is a drop-down menu with ID dropdownlist1 on the page ):
1<% @OutputcacheDuration = "60"Varybyparam = "NONE"Varybycontrol = "dropdownlist1"%>
As mentioned above, all pages are configured with page caching on the front-end page.. Net also provides page caching through background code. The cache policy of the page output cache API is to use the cache attribute of response. The core of this method is to call system. Web. httpcachepolicy. This class mainly includes methods for users to set specific HTTP headers to cache and for controlling the ASP. NET page output cache. The following describes several common methods:
SetexpiresUsed to set the absolute cache expiration time. Its parameter is an instance of the datatime class, indicating the absolute expiration time.
SetlastmodifiedUsed to set the last-modified HTTP header of the page. The last-modified HTTP header indicates the last modification time of the page, and the cache will rely on it for timing. This method fails if the cache restriction hierarchy is violated. The parameter of this method is an instance of the datatime class.
SetslidingexpirationThis method sets the cache expiration time from the absolute time to the adjustable time. The parameter is a Boolean value. When the parameter is true, the cache-control HTTP header is updated with each response. This expiration mode is the same as the IIS configuration option that adds the expiration header to all output sets relative to the current time. When the parameter is set to false, this setting is retained, and any attempt to enable and adjust the expiration time will still fail. This method is not directly mapped to the HTTP header. It is used by subsequent modules or auxiliary requests to set the Cache Policy of the source server.
SetomitvarystarAdded methods for ASP. NET 2.0. It is used to specify whether the response should contain the vary: * Header when partitioning by parameters. The method parameter is a Boolean value. To indicate that httpcachepolicy does not use the * value for its varybyheaders attribute, the value is true; otherwise, the value is false.
SetcacheabilitySet the cache-control HTTP header of the page. This header is used to control the way documents are cached on the network. This method has two overload methods, with different parameters. The parameters of one overload method are the httpcacheability enumeration values, including nocache, private, public, server, serverandnocache, and serverandprivate (for definitions of these enumeration values, refer to msdn ). The other method has two parameters: one is the httpcacheability enumeration value and the other is a string, indicating the Cache Control extension added to the header. Note that the field extension is valid only when used together with the private or nocache command. If the combination of incompatible commands and extensions, this method will cause invalid parameter exceptions.