ASP.NET 3.5 : Caching ASP.NET Pages (part 4) - Advanced Caching Features

2/18/2013 6:30:52 PM

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:

          <add name="..." enabled="true|false" duration="..."
               location="..." sqlDependency="..."
               varyByCustom="..." varyByControl="..."
               varyByHeader="..." varyByParam="..."

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" />

Figure 4 shows the page in action.

Figure 4. The fragment of the page between the two horizontal lines is updated regularly at each postback; the rest of the page is retrieved from the cache.

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:

    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.


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.

Top 10
Review : Sigma 24mm f/1.4 DG HSM Art
Review : Canon EF11-24mm f/4L USM
Review : Creative Sound Blaster Roar 2
Review : Philips Fidelio M2L
Review : Alienware 17 - Dell's Alienware laptops
Review Smartwatch : Wellograph
Review : Xiaomi Redmi 2
Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
Popular Tags
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone