Realize
To implement the page output cache, simply add a OutputCache directive to the page.
<%@ OutputCache duration= "*" varybyparam= "*"%>
As with other page directives, the directive should appear at the top of the ASP Tutorial x page, before any output. It supports five properties (or parameters), of which two are required.
Duration
Required Properties. The time, in seconds, that the page should be cached. Must be a positive integer.
Location
Specifies where the output should be cached. If you want to specify this parameter, you must be one of the following: Any, client, downstream, none, Server, or ServerAndClient.
VaryByParam
Required Properties. The name of the variable in request, which should produce a separate cache entry. "None" indicates no change. The "*" can be used to create a new cache entry for each variant array. separated by ";" between variables.
VaryByHeader
Changes the cached entry based on the changes in the specified header.
VaryByCustom
Allows you to specify a custom change (for example, "browser") in Global.asax.
Use a combination of the required duration and VaryByParam options to handle most situations. For example, if your product catalog allows users to view catalog pages based on CategoryID and page variables, you can cache the product catalog for a period of time with the VaryByParam parameter value of "Categoryid;page" (if the product is not always changing, an hour is acceptable, Therefore, the duration is 3,600 seconds). This creates a separate cache entry for each catalog page of each kind. Each entry will be maintained for one hours from its first request.
VaryByHeader and VaryByCustom are primarily used to customize the appearance or content of a page based on the client accessing the page. The same URL may need to render output for both the browser and the mobile client, so different content versions are cached for different clients. Alternatively, the page may have been optimized for IE, but it needs to be able to completely reduce optimizations (rather than just damaging pages) against Netscape or Opera. The latter example is very common, and we will provide an example that shows how to achieve this goal:
Example: VaryByCustom is used to support browser customization
In order for each browser to have a separate cache entry, the VaryByCustom value can be set to "browser". This feature is already built into the cache module, and a separate page cache version is inserted for each browser name and major version.
<%@ OutputCache duration= varybyparam= "None" varybycustom= "Browser"%>
Fragment caching, user control output caching
Caching an entire page is usually not feasible, because some parts of the page are customized for the user. However, other parts of the page are common to the entire application. These sections are best used for caching with fragment caching and user controls. Menus and other layout elements, especially those that are dynamically generated from the data source, should also be cached in this way. If you want, you can configure the cached control to change based on changes to its controls (or other properties) or any other changes supported by the page-level output cache. Hundreds of pages that use the same set of controls can also share cached entries for those controls, rather than reserving separate cached versions for each page.