Using the @ OutputCache directive
With the @ OutputCache directive, the general need for page output caching can be achieved. The @ OutputCache directive declares the header of the user control contained in an ASP. NET page or page. This is a convenient way to implement a page's output caching strategy with just a few simple property settings. The @ OutputCache directive declaration code is as follows.
@ OutputCache Instruction Code
<%@ OutputCache cacheprofile = "" 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, in the @ OutputCache directive, there are 10 properties, which are CacheProfile, Nostore, Duration, GKFX, location, SqlDependency, VaryByControl, VaryByCustom, VaryByHeader and VaryByParam. These properties set the cache time, the location of the cache entry, the SQL data cache dependency, and so on. The basic concepts of the above properties are briefly described below.
CacheProfile
Used to define the name of the cache settings associated with the page. is an optional property and the default value is a null character (""). It is important to note that the @ OutputCache directive contained in the user control does not support this property. When you specify this property in a page, the property value must match the name of one of the available items in the outputCacheProfiles element under the <outputCacheSettings> configuration section of the Web. config file. If this name does not match the configuration file, an exception is thrown.
Nostore
This property defines a Boolean value that determines whether to block level two storage of sensitive information. It is important to note that the @ OutputCache directive contained in the user control does not support this property. Setting this property to true is equivalent to executing the code during the request "Response.Cache.SetNoStore ();".
Duration
Used to set the page or user control cache time. Unit is seconds. By setting this property, you can establish an expiration policy for the HTTP response from the object and automatically cache the page or user control output. It is important to note that the Duration property is required, otherwise it will cause a parser error.
Shared
This property defines a Boolean value that determines whether the user control output can be shared by more than one page. The default value is False. Note that this property is not supported by the @ OutputCache directive contained in the ASP.
Location
Used to specify the location of the output cache entry. Its property values are OutputCacheLocation enumeration values, which are any, Client, downstream, None, server, and ServerAndClient. The default value is any, which indicates that the output cache is available for all requests, including the client browser, proxyServer or on the server that is processing the request. It is important to note that the @ OutputCache directive contained in the user control does not support this property.
SqlDependency
This property identifies a set of string values for a database/table name pair, and the output cache of a page or control depends on these name pairs. Note: The SqlCacheDependency class monitors the tables in the database that the output cache relies on, so when items in the table are updated, using table-based polling will remove those items from the cache. When the notification (in SQL Server 2005) is used with the CommandNotification value, the SqlDependency class is eventually used to register the query notification with the SQL Server 2005 server. In addition, the CommandNotification value of the SqlDependency property is valid only in ASP. Control can only use table-based polling for the @ OutputCache directive.
VaryByControl
This property uses a semicolon-delimited list of strings to change the output cache of the user control. These strings represent the ID property values of the ASP. NET server control declared in the user control. This property is required in the @ OutputCache directive unless the VaryByParam property is already included.
VaryByCustom
Any text that is used to customize the output caching requirements. If the property value is given as browser, the cache will vary depending on the browser name and the major version information. If you enter a custom string, you must override the HttpApplication.GetVaryByCustomString method in the application's Global.asax file.
VaryByHeader
This property contains a semicolon-delimited list of HTTP headers that are used to make the output cache vary. When this property is set to multiple headers, the output cache contains a different version of the requested document for each specified header. The VaryByHeader property enables cache entries in all HTTP 1.1 caches, not just the ASP. This property is not supported by the @ OutputCache directive in the user control.
VaryByParam
This property defines a semicolon-delimited list of strings that are used to make the output cache vary. By default, these strings correspond to the query string values sent with the Get method property, or to the parameters sent with the Post method. When the property is set to multiple parameters, the output cache contains a different version of the requested document for each specified parameter. Possible values include "none", "*", and any valid query string or post parameter name. It is important to note that this property is required when the output cache is an ASP. It is also required for a user control unless the VaryByControl property has been included in the user control's @ OutputCache directive. If it is not included, a parser error occurs. If you do not need to make the cached content change with any of the specified parameters, you can set the value to "none". If you want the output cache to change based on all parameter values, set the property to "*".
The role of each parameter of the OutputCache directive in ASP.