. Net2.0 page Cache

Source: Internet
Author: User

After a long time, I went to the IE cache to find the bug. It was not an issue with IE. The Code went wrong. If I was depressed, I was so worried that it was too cool, if you are careful, you will not be able to solve this problem. Haha, but fortunately, I have reviewed some of the cache knowledge from the beginning to the end. I have no intention to find the following article. It is quite good, share with you .........

 

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. When there are other requests, the cache output will be provided until the cache expires. In this way, performance may be greatly improved (depending on the overhead required to create the original page output-the output of the sending cache is always fast and stable ).
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.
@ OutputcacheControl the output cache policies of user controls contained in ASP. NET pages or pages in a declarative manner.
Syntax:

<% @ 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"
%>
Duration
Page or user control cache time (in seconds ). Set this attribute on the page or user control to create an expiration Policy for the HTTP response from the object and automatically cache the page or user control output.
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.
Shared
A boolean value to determine whether the output of the user control can be shared by multiple pages. The default value is False.
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 ();".
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 "*".
Example<% @ Outputcache duration = "120" varybyparam = "categoryid; selectedid" %>

The code above sets the cache validity period of the user control to 120 seconds, and allows you to use the categoryid and selectedid parameters to change the cache.

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

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.

<% @ Outputcache duration = "120" varybyparam = "NONE" varybycontrol = "category" %>

The above Code sets the cache validity period to 120 seconds, and the page does not change with any get or post parameters (even if the varybyparam attribute is not used, it still needs to be explicitly declared in the @ outputcontrol command ). If a user control contains a server control with the ID attribute "category" (for example, a drop-down box control), the cache stores the user control data based on the control changes.
Use the page output cache API
The core of this method is to call system. Web. httpcachepolicy. This class mainly includes methods used to set cache-specific HTTP headers and to control the ASP. NET page output cache. Compared with the httpcachepolicy class in. NET Framework 1. X, the httpcachepolicy class in. NET Framework 2.0 has been expanded and developed. It mainly adds some important methods, such as the setomitvarstar method. As there are many httpcachepolicy class methods, the following describes some common methods.
Setexpires Method
Used to set the absolute cache expiration time. Its parameter is an instance of the datatime class, indicating the absolute expiration time.
Setlastmodified Method
Used 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.
 Setslidingexpiration Method  
This 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.
 Setomitvarystar Method
Added 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.
Setcacheability Method
Set 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.
Example

Response. cache. setexpires (datetime. Now. addseconds (60 ));
Response. cache. setexpires (datetime. parse ("6:00:00 "));

The cache attribute of the response class is used to obtain the page Cache Policy. The data type of this attribute is httpcachepolicy. You can call response. cache to obtain the httpcachepolicy instance, and then set the output cache of the current page. As shown in the above Code, the first line of code indicates that the output cache time is 60 seconds, and the page does not change with any get or post parameters, equivalent to "<% @ outputcache duration =" 60 "varybyparam =" NONE "%> ". The second line of code sets the absolute cache expiration time to six o'clock P.M. on the same day

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.