5. Advanced Caching Features
The output caching subsystem has also a few
other cool features to offer. They are caching profiles and post-cache
substitution. In brief, caching profiles let you save a block of output
caching-related settings to the configuration file. Post-cache
substitution completes the ASP.NET offering as far as output caching is
concerned. In addition to saving the entire page or only fragments of
the page, you can now also cache the entire page except for a few
regions.
Caching Profiles
The @OutputCache directive for pages supports the CacheProfile string attribute, which references an entry under the <outputCacheProfiles> section in the web.config file:
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="..." enabled="true|false" duration="..."
location="..." sqlDependency="..."
varyByCustom="..." varyByControl="..."
varyByHeader="..." varyByParam="..."
noStore=true|false"
/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
Basically, by defining a named entry in the <add>
section you can store in the configuration file all the cache-related
settings to be applied to the page. Instead of specifying the same set
of parameters for each page over and over again, you can put them in the
web.config file and reference them by
name. In this way, you can also modify settings for a number of pages
without touching the source files.
<%@ OutputCache CacheProfile="MySettings" %>
In the preceding code, the application has a MySettings entry in the <outputCacheProfiles> section and doesn’t need any additional attribute in the @OutputCache directive. As you can see, the attributes of the <add> node match the attributes of the @OutputCache directive.
Post-Cache Substitution
With user controls, you can cache only certain
portions of ASP.NET pages. With post-cache substitution, you can cache
the whole page except specific regions. For example, using this
mechanism, an AdRotator control can serve a different advertisement on each request even if the host page is cached.
To use post-cache substitution, you place a new control—the <asp:substitution> control—at the page location where content should be substituted, and you set the MethodName property of the control to a callback method. Here’s a quick example:
<form id="form1" runat="server">
<h3>The output you see has been generated at:
<%=DateTime.Now.ToString() %> and is valid for 30 seconds</h3>
<hr />
This content is updated regularly
<h2><asp:Substitution ID="Substitution1" runat="server"
MethodName="WriteTimeStamp" /></h2>
<hr />
This is more static and cached content
<asp:Button runat="server" Text="Refresh" />
</form>
Figure 4 shows the page in action.
The MethodName property must be set to the name of a static method that can be encapsulated in an HttpResponseSubstitutionCallback delegate, as follows:
public static string WriteTimeStamp(HttpContext context)
{
return DateTime.Now.ToString();
}
Whatever string the method returns will be rendered out and becomes the output of the Substitution control. Note also that the callback method must be static and thread-safe. The HttpContext
parameter to the method can be used to retrieve request parameters such
as query string variables, authentication information, or
personalization details.
You can also set up post-cache substitution programmatically through the WriteSubstitution method of the HttpResponse object:
Response.WriteSubstitution(
new HttpResponseSubstitutionCallback(WriteTimeStamp));
The preceding call inserts a sort of
placeholder in the response, which will be replaced with the output of
the method. This trick allows the AdRotator control in ASP.NET 2.0 to automatically display a new banner even on cached pages.
The use of post-cache substitution
automatically enables server caching for the page output. If the page is
configured for client output caching, the setting is ignored. The
reason for this change lies in the fact that markup editing is
necessarily a server-side operation. In addition, a page that makes use
of post-cache substitution can’t rely on IIS 6.0 kernel mode caching
because ASP.NET needs to do some work before the page can be served to
the user. In light of this, the page can’t just be served by IIS without
first involving ASP.NET.
Note
The Substitution
control can also be used in pages that don’t use output caching. In
this case, the substitution callback will be called at rendering time to
contribute to the response. You can think of the Substitution control as a server-side control that has the capability of expanding a placeholder to some server-side processed results. |
For performance reasons, you should also avoid calling the Substitution
control from within the callback. If you do so, the callback will
maintain a reference to the control and the page containing the control.
As a result, the page instance won’t be garbage-collected until the
cached content expires.