ASP.net 2.0 page Output Caching _ Practical Tips

Source: Internet
Author: User
Tags server memory
Static page full content is saved in server memory. When there is another request, the system outputs the relevant data in the cache directly until the cached data expires. In this process, the cache does not need to go through the page processing lifecycle again. This reduces request response time and improves application performance. Obviously, the page output cache is suitable for pages that do not need to update data frequently, and that consume a lot of time and resources to compile a build. For pages where the data is often updated, it does not apply. By default, ASP.net 2.0 enables page output caching, but does not cache the output of any response. The developer must pass the settings so that some of the page's responses become part of the cache.

There are two ways to set up page output caching: One is to use the @ OutputCache directive and the other is to use the page output caching API. The @ OutputCache directive was once seen in ASP.net 1.x and has been inherited and enhanced in ASP.net 2.0. The page output caching API mainly refers to the HttpCachePolicy class.

using the @ OutputCache directive

With the @ OutputCache directive, you can implement a general need for page output caching. The @ OutputCache directive declares the head of a user control contained in a ASP.net page or page. This approach is very convenient, with just a few simple property settings, you can implement the page's output caching strategy. The @ OutputCache directive declares the following code.


@ OutputCache指令代码

<%@ 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, there are 10 properties in the @ OutputCache directive, which are CacheProfile, Nostore, Duration, Shared, Location, SqlDependency, VaryByControl, VaryByCustom, VaryByHeader and VaryByParam. These attributes 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

The name used to define the cache settings associated with the page. is an optional property and the default value is a null character (""). Note that the @ OutputCache directive contained in the user control does not support this property. When this property is specified in the page, the property value must match the name of one of the available items in the outputCacheProfiles element in the Web.config file <outputCacheSettings> configuration section. If this name does not match the configuration file entry, an exception is thrown.

Nostore

This property defines a Boolean value that determines whether to block level two storage of sensitive information. Note that the @ OutputCache directive contained in the user control does not support this property. Setting this property to true is equivalent to executing code "Response.Cache.SetNoStore ()" during the request.

Duration

Time to set the page or user control cache. The unit is seconds. By setting this property, you can establish an expiration policy for HTTP responses from objects and automatically cache page or user control output. It should be noted that the Duration property is required, or it will cause a parser error.

Shared

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

Location

Used to specify the location of the output cache entry. Their property 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 for all requests, including the client browser, the proxy server, or the server that handles the request. Note that the @ OutputCache directive contained in the user control does not support this property.

SqlDependency

This property identifies the string value of a set of database/table name pairs, and the output caching of the 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, the table based polling is used to remove the items from the cache. When notified (in SQL Server 2005) is used with the CommandNotification value, the SqlDependency class is eventually used to register query notifications with the SQL Server 2005 server. In addition, the CommandNotification value of the SqlDependency property is valid only in the ASP.net page. Control can only use a table based poll 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 attribute 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 you give the property value a 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 change the output cache. 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 limited to the asp.net cache. 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 change the output cache. By default, these strings correspond to the query string values sent with the Get method property, or to parameters that are sent with the Post method. When this 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 caches asp.net pages. It is also required for user controls, unless the VaryByControl attribute is already included in the @ OutputCache directive of the user control. If it is not included, a parser error occurs. If you do not want the cached content 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 "*".

The following is a list of two sample codes that use the @outputcache directive.


Using the @ OutputCache sample code 1

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

The above example is the 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 that are received while the page is still cached are serviced by the cached data. After 100 seconds, the page data is removed from the cache and then the next request is explicitly processed and the page is cached again.

Using the @ OutputCache sample code 2


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


The above @ OutputCache directive sets the expiration date of the page output cache to 100 seconds and sets the output cache according to the query string parameter location or FirstName. For example, assuming that the client request is "http://localhost/default.aspx?location=beijing", the page will be treated as a cache.

The

Static page is all stored in server memory. When there is another request, the system outputs the relevant data in the cache directly until the cached data expires. In this process, the cache does not need to go through the page processing lifecycle again. This reduces request response time and improves application performance. Obviously, the page output cache is suitable for pages that do not need to update data frequently, and that consume a lot of time and resources to compile a build. For pages where the data is often updated, it does not apply. By default, ASP.net 2.0 enables page output caching, but does not cache the output of any response. The developer must pass the settings so that some of the page's responses become part of the cache. The

Setting page output cache can be used in two ways: one is using the @ OutputCache directive and the other is using the page output caching API. The @ OutputCache directive was once seen in ASP.net 1.x and has been inherited and enhanced in ASP.net 2.0. The page output caching API mainly refers to the HttpCachePolicy class.

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.