SoFunction
Updated on 2025-03-08

Page output cache in 2.0

All contents of the static page are saved in the server memory. When there is another request, the system outputs the relevant data in the cache directly until the cached data expires. During this process, the cache does not need to go through the page processing life cycle again. This can shorten request response time and improve application performance. Obviously, the page output cache is suitable for pages that do not require frequent updates of data, but take up a lot of time and resources to compile generated pages. Not applicable for pages where data is frequently updated. By default, 2.0 enables page output caching, but does not cache any response output. Developers must make the response of certain pages part of the cache by setting them.

You can use the following two ways to set up page output cache: one is to use the @OutputCache directive, and the other is to use the page output cache API. The @OutputCache directive has appeared in , and has been inherited and enhanced in 2.0. The page output cache API mainly refers to the HttpCachePolicy class.

Use @OutputCache Directive

Using the @OutputCache directive, the general need for page output cache can be achieved. @OutputCache directive The header declaration of the user control contained in the page or page. This method is very convenient. It only requires a few simple attribute settings to implement the output cache strategy of the page. @OutputCache directive declaration code is as follows.


@OutputCache directive 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 in total, which are CacheProfile, NoStore, Duration, Shared, Location, SqlDependency, VaryByControl, VaryByCustom, VaryByHeader, and VaryByParam. These properties will set cache time, cache item location, SQL data cache dependency and other aspects. The following briefly introduces the basic concepts of the above attributes.

CacheProfile

The name of the cache setting associated with the page. is an optional attribute, with a default value of null characters (""). It should be noted that the @OutputCache directive included in the user control does not support this property. When specifying this property on the page, the property value must match the name of an available item in the outputCacheProfiles element under the file <outputCacheSettings> configuration section. If this name does not match the configuration file item, an exception is thrown.

NoStore

This property defines a Boolean value that determines whether to block secondary storage of sensitive information. It should be noted that the @OutputCache directive included in the user control does not support this property. Setting this property to true is equivalent to executing the code "();" during the request.

Duration

Used to set the time for page or user control cache. The unit is seconds. By setting this property, an expiration policy can be established for HTTP responses from the object and the page or user control output will be automatically cached. It should be noted that the Duration property is required, otherwise it will cause an analyzer error.

Shared

This property defines a Boolean value that determines whether the user control output can be shared by multiple pages. The default value is false. Note that the @OutputCache directive included in the page does not support this property.

Location

Used to specify the location of the output cache item. Its property values ​​are OutputCacheLocation enum values, which are Any, Client, Downstream, None, Server, and ServerAndClient. The default value is Any, indicating that the output cache can be used on all requests, including on the client browser, proxy server, or on the server that handles the request. It should be noted that the @OutputCache directive included in the user control does not support this property.

SqlDependency

This property identifies the string values ​​of a set of database/table name pairs on which the output cache of the page or control depends. Note: The SqlCacheDependency class monitors tables in the database on which the output cache depends, so when updating items in a table, using table-based polling will remove these items from the cache. When notifications (in SQL Server 2005) are used with CommandNotification values, query notifications are eventually registered with the SQL Server 2005 server using the SqlDependency class. In addition, the CommandNotification value of the SqlDependency property is only valid in the page. Controls can only use table-based polling for @OutputCache directives.

VaryByControl

This property uses a semicolon-delimited list of strings to change the output cache of the user control. These strings represent the ID attribute values ​​of the server control declared in the user control. Unless the VaryByParam property is already included, this property is required in the @OutputCache directive.

VaryByCustom

Used to customize the output cache requirements. If the attribute value is given as browser, the cache will vary depending on the browser name and main version information. If a custom string is entered, the method must be overridden in the application's file.

VaryByHeader

This property contains a semicolon-delimited list of HTTP headers to change the output cache. When this property is set to multi-header, the output cache contains a different version of the request document for each specified header. The VaryByHeader property enables cache items in all HTTP 1.1 caches, not just caches. This property is not supported by the @OutputCache directive in the user control.

VaryByParam

This property defines a semicolon-separated list of strings to change the output cache. By default, these strings correspond to the query string value sent with the GET method attribute, or to the parameters sent with the POST method. When this property is set to multi-parameter, the output cache contains a different version of the request document for each specified parameter. Possible values ​​include "none", "*" and any valid query string or POST parameter name. It is worth noting that this property is required when outputting cache pages. It is also necessary for user controls unless the VaryByControl property is already included in the @OutputCache directive of the user control. If not included, an analyzer error occurs. If there is no need to change the cached content with any specified parameters, the value can be set to "none". If you want the output cache to change based on all parameter values, set the property to "*".

The following are two example codes using the @OutputCache directive.


Example code using @OutputCache 1

<%@ OutputCache Duration="100" VaryByParam="none"%>

The above example is the basic application of the @OutputCache directive, which indicates that the validity period of the page output cache is 100 seconds and that the page does not change with any GET or POST parameters. The requests received while the page is still cached are served by the cached data. After 100 seconds, the page data is removed from the cache, and the next request is then explicitly processed and the page is cached again.

Example code using @OutputCache 2


<%@ OutputCache Duration="100" VaryByParam="location;firstname" %>


The above @OutputCache instruction sets the validity period of the page output cache is 100 seconds, and the output cache is set according to the query string parameter location or firstname. For example, suppose the client request is "http://localhost/?location=beijing", then the page will be processed as a cache.

All contents of the static page are saved in the server memory. When there is another request, the system outputs the relevant data in the cache directly until the cached data expires. During this process, the cache does not need to go through the page processing life cycle again. This can shorten request response time and improve application performance. Obviously, the page output cache is suitable for pages that do not require frequent updates of data, but take up a lot of time and resources to compile generated pages. Not applicable for pages where data is frequently updated. By default, 2.0 enables page output caching, but does not cache any response output. Developers must make the response of certain pages part of the cache by setting them.

You can use the following two ways to set up page output cache: one is to use the @OutputCache directive, and the other is to use the page output cache API. The @OutputCache directive has appeared in , and has been inherited and enhanced in 2.0. The page output cache API mainly refers to the HttpCachePolicy class.