Post-Cache Substitution for optimizing the cached pages

Hi

The partial page caching approach works very well for scenarios where most of the page content is dynamic and/or it is easy to encapsulate cached content into isolated user controls. The concept does not work well for the reverse scenario, when most of page content should be cached and only a very small portion is dynamically built. For example, consider a page displaying static data but only the username displayed is dynamic. In this case it might not be possible to cache the entire page because of the username, which is dynamic.

Post-Cache Substitution is the answer for the above situation. This control is available in asp.net 2.0. This feature is aimed at optimizing the cached pages. Earlier we would use User Control to cache regions in the page. But with Post Cache substitution we can cache a full page and allow a small part of the page to be updated dynamically. For this we have to identify the regions that should be exempted from caching.

We can place a substitution control (server side control) where dynamic content is required. We have to set the MethodName property to the callback method on the page. This method has to be a static method on the controls container.

The code for working with substitution control is

<%@ OutputCache Duration="60" VaryByParam="none" %>
<script language="C#" runat="server">
 
public static String GetTheCurrentTime (HttpContext context) {
   
return System.dateTime.Now.ToString();
 
}
</script>
Some static Cached content
<form runat="server">
 
<h2>The current Time is  <asp:Substitution MethodName="GetTheCurrentTime" />!</h2>
</form>

Here I am returning the current time but with the HttpContext parameter to the callback we can also retrieve the parameters like query string, Session variable, authentication detail etc.

Hope this helps
Thanks
Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Posted on 11/2/2006 8:59:22 AM

Hey, I just found your blog today and wanted to say that I've found it really useful, this post most of all so far. I was running into the exact dilema you stated here, I wanted to cache almost the entire page exact the user's email. The only option I could come up with was making everything in a user control but that would have been a pain. Thanks for the tip!

Posted on 7/9/2008 3:26:50 AM

Hi Vikram,
this is great article for substitution control. In this article to learn Caching the web page and how will be handle it.

Thanks
santosh Kumar
http://www.operativesystems.com

Posted on 7/9/2008 3:27:18 AM

Hi Vikram,
this is great article for substitution control. In this article to learn Caching the web page and how will be handle it.

Thanks
santosh Kumar
http://www.operativesystems.com

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2009 Vikram Lakhotia