Programming Microsoft ASP.NET 4 - Dino Esposito [395]
A much better strategy is to create the page once, cache it somewhere, and give the page response a maximum duration. When the cached page becomes stale, the first incoming request will be served in the standard way, running the page’s code, and the new page output will be cached for another period until it also becomes stale.
ASP.NET page output caching is the feature that allows you to control the cache-related behavior of the page. Output caching can take place at two levels: for entire pages or for portions of the page. Page caching is smart enough to let you save distinct output based on the requesting URL, query string, or form parameters, and it lets you choose the location and duration of the cache. The console through which you control all of this is the @OutputCache directive.
The @OutputCache Directive
Just like other page directives, @OutputCache goes at the top of the ASP.NET page. The directive allows you to specify a handful of attributes, a couple of which—Duration and VaryByParam—are mandatory. The Duration attribute indicates in seconds how long the page output should stay in the cache. The VaryByParam attribute allows you to vary the cached output depending on the GET query string or form POST parameters. The following declaration indicates the page should stay in the cache for one minute regardless of any GET or POST parameters:
<%@ OutputCache Duration="60" VaryByParam="None" %>
For frequently requested pages and relatively static pages, the @OutputCache directive is a real performance booster. With a shorter duration, even limited to one second or two, it provides a way to speed up the entire application.
Available attributes indicate the location of the cache, its duration, and the arguments to use to vary page caching. The list of supported attributes is shown in Table 18-8. Note that the directive can be applied to both pages (.aspx) and user controls (.ascx). Note that some of the attributes are valid in one case but not the other.
Table 18-8. Attributes of the @OutputCache Directive
Attribute
Applies to
Description
CacheProfile
Page
Associates a page with a group of output caching settings specified in the web.config file. (More details about this appear later in the chapter.)
Duration
Page, User control
The time, in seconds, that the page or user control is cached.
Location
Page
Specifies the location (browser, proxy, or server) where to store the output of a page. The attribute takes its value from the OutputCacheLocation enumeration.
NoStore
Page
Indicates whether to send a Cache-Control:no-store header to prevent browser-side storage of the page output.
Shared
User control
Indicates whether the user control output can be shared with multiple pages. It is false by default.
SqlDependency
Page, User control
Indicates a dependency on the specified table on a given SQL Server database. Whenever the contents of the table changes, the page output is removed from the cache.
VaryByControl
User control
A semicolon-separated list of strings that represent properties of the user control. Each distinct combination of values for the specified properties will originate a distinct copy of the page in the cache.
VaryByCustom
Page, User control
A semicolon-separated list of strings that lets you maintain distinct cached copies of the page based on the browser type or user-defined strings.
VaryByHeader
Page
A semicolon-separated list of HTTP headers.
VaryByParam
Page, User control
A semicolon-separated list of strings representing query string values sent with GET method attributes, or parameters sent using the POST method.
Note that the VaryByParam attribute is mandatory. If you omit it, a runtime exception is always thrown. However, if you don’t need to vary by parameters, set the attribute to None. The empty string is not an acceptable value for the VaryByParam attribute.
Choosing a Location for the Page Output
Among other things, you use the @OutputCache directive to decide where the page output should be cached.