Page 297 - DCAP408_WEB_PROGRAMMING
P. 297
Unit 13: ASP Cookies and Caching
Notes
Task Analyze the advantages of caching?
13.5.1 Fragment Caching, User Control Output Caching
Often, caching an entire page is not feasible, because certain parts of the page are customized for
the user. However, there may be other parts of the page that are common to the entire application.
These are perfect candidates for caching, using fragment caching and user controls. Menus and
other layout elements, especially ones that are dynamically generated from a data source,
should be cached with this technique. If need be, the cached controls can be configured to vary
based on the changes to its controls (or other properties) or any of the other variations supported
by page level output caching.
!
Caution Hundreds of pages using the same controls can also share the cached entries for
those controls, rather than keeping separate cached versions for each page.
Implementation
Fragment caching uses the same syntax as page level output caching, but applied to a user control
(.ascx file) instead of to a web form (.aspx file). All of the attributes supported by the OutputCache
directive on a web form are also supported for user controls except for the Location attribute. User
controls also support an OutputCache attribute called VaryByControl, which will vary the caching
of the user control depending on the value of a member of that control (typically a control on the
page, such as a DropDownList). If VaryByControl is specified, VaryByParam may be omitted.
Finally, by default each user control on each page is cached separately. However, if a user control
does not vary between pages in an application and is named the same across all such pages, the
Shared=”true” parameter can be applied to it, which will cause the cached version(s) of the user
control to be used by all pages referencing that control.
Example:
<%@ OutputCache Duration=”60" VaryByParam=”*” %>
This would cache the user control for 60 seconds, and would create a separate cache entry for
every variation of querystring and for every page this control is placed on.
<%@ OutputCache Duration=”60" VaryByParam=”none”
VaryByControl=”CategoryDropDownList” %>
This would cache the user control for 60 seconds, and would create a separate cache entry for each
different value of the CategoryDropDownList control, and for each page this control is placed on.
<%@ OutputCache Duration=”60" VaryByParam=”none” VaryByCustom=”browser”
Shared=”true %>
Finally, this would cache the user control for 60 seconds, and would create one cache entry for each
browser name and major version. The cache entries for each browser would then be shared by all
pages referencing this user control (as long as all pages refer to the control with the same ID).
13.5.2 Page Caching
Page caching is an approach to caching where the entire action output of is stored as a HTML file
that the web server can serve without going through Action Pack. This is the fastest way to cache
LOVELY PROFESSIONAL UNIVERSITY 291