Page output caching in ASP.NET 2.0

  • 2020-05-05 11:08:12
  • OfStack

The entire contents of the static page are stored in server memory. When another request is made, the system outputs the relevant data directly from the cache until the cache data expires. In this process, the cache does not need to go through the page processing life cycle again. This can reduce request response time and improve application performance. Obviously, page output caching is good for pages that don't require frequent updates of data and take a lot of time and resources to compile. This is not true for pages where the data is frequently updated. By default, ASP.NET 2.0 enables page output caching, but does not cache the output of any response. The developer must set up the response for certain pages to be part of the cache.

There are two ways to set up the 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 previously appeared in ASP.NET 1.x and has been inherited and enhanced in ASP.NET 2.0. Page output cache API mainly refers to the HttpCachePolicy class.

USES the @OutputCache directive

The general need for page output caching can be achieved using the @OutputCache directive. The @OutputCache directive is declared in the header of the ASP.NET page or the user control contained in the page. This is a convenient way to implement a page's output caching strategy with a few simple property Settings. The @OutputCache directive declares the following code.


@OutputCache directive code

< % @OutputCache CacheProfile =" " NoStore= "True esb0 False ="#ofseconds" ="True | False Location ="Any | Client esb3 Downstream | customstring | VaryByHeader VaryByHeader ="headers" VaryByParam ="parametername" % >

as shown above, in the @OutputCache directive, there are 10 attributes, CacheProfile, NoStore, Duration, Shared, Location, SqlDependency, VaryByControl, VaryByCustom, VaryByHeader, and VaryByParam. These attributes are set to cache time, cache item location, SQL data cache dependency, and so on. The following is a brief introduction to the basic concepts of the above attributes.

CacheProfile

The name used to define the cache Settings associated with the page. Is an optional property and the default value is null (""). It is important to note that the @OutputCache directive contained in the user control does not support this property. When this property is specified on the page, the property value must match the name of an available item in the outputCacheProfiles element under the Web.config file < outputCacheSettings > configuration section. If this name does not match the profile entry, an exception is thrown.

NoStore

This property defines a Boolean value to determine whether to block secondary 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 "Response.Cache.SetNoStore () during the request;" .

Duration

Time to set the page or user control cache. It's in 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 attribute is required or it will cause an parser error.

Shared

This property defines a Boolean value to determine whether the output of the user control can be Shared by multiple pages. The default value is false. Note that the @OutputCache directive contained on the ASP.NET page does not support this property.

Location

Used to specify the location of the output cache item. The attribute values are OutputCacheLocation enumerated values, which are Any, Client, Downstream, None, Server, and ServerAndClient. The default value is Any, which means that the output cache is available on all requests, including the client browser, the proxy server, or the server that handles 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 string value for a set of database/table name pairs on which the output cache of a page or control depends. Note: the SqlCacheDependency class monitors the tables in the database on which the output cache depends, so when updating the items in the table, using table-based polling removes them 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 for the SqlDependency attribute is only valid on the ASP.NET page. 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. The VaryByParam attribute is required in the @OutputCache directive unless it already contains it.

VaryByCustom

Any text used to customize the output cache requirements. If the attribute value is browser, the cache will vary with the browser name and 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-separated list of HTTP headers for making changes to the output cache. When this property is set to multiple headers, the output cache contains a different version of the request document for each specified header. The VaryByHeader attribute enables cache entries in all HTTP 1.1 caches, not just ASP.NET caches. The @OutputCache directive in the user control does not support this property.

VaryByParam

This property defines a semicolon-separated list of strings for making changes to the output cache. By default, these strings correspond to query string values sent with the GET method property, or to parameters sent with the POST method. When this property is set to multiple parameters, 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 names. It is worth noting that this property is required when the output caches the ASP.NET page. It is also required for user controls, unless the VaryByControl property is already included in the user control's @OutputCache directive. If not, an parser error occurs. If you do not want the cache contents to 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 "*".

Here are two examples of code that use the @OutputCache directive.


Example code 1
using @OutputCache
< % @OutputCache Duration="100" VaryByParam="none"% >

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

Example code 2
using @OutputCache

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


The above @OutputCache directive sets the page output cache to be valid for 100 seconds and sets the output cache based on the query string parameters location or firstname. For example, assume that the client request is "http: / / localhost default aspx? location=beijing ", then the page will be processed as a cache.

The entire contents of the static page are stored in server memory. When another request is made, the system outputs the relevant data directly from the cache until the cache data expires. In this process, the cache does not need to go through the page processing life cycle again. This can reduce request response time and improve application performance. Obviously, page output caching is good for pages that don't require frequent updates of data and take a lot of time and resources to compile. This is not true for pages where the data is frequently updated. By default, ASP.NET 2.0 enables page output caching, but does not cache the output of any response. The developer must set up the response for certain pages to be part of the cache.

There are two ways to set up the 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 previously appeared in ASP.NET 1.x and is inherited and enhanced in ASP.NET 2.0. Page output cache API mainly refers to the HttpCachePolicy class.


Related articles: