Page output caching in asp.net 2.0

Source: Internet
Author: User
Tags table name 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.

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.