Caching in ASP.NET (Part I)

 

Page Fragment Caching

 
Also known as Partial Page Caching, this is a feature that allows specific portions of the web page to be cached rather than caching the entire web page and is useful in situations where a particular web page can contain both static and dynamic content. MSDN says, “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“. The term “fragment” implies one or more user controls in the web page. Note that each of these user controls can have different cache durations. The following statement depicts how this can be accomplished.
<%@ OutputCache Duration=”10″
VaryByControl=”PatientID” VaryByParam=”*”%>

New Page Caching Features in ASP.NET 2.0

 
With ASP.NET 2.0, you do no longer need to split your pages into multiple .ascx user control files for implementing Page Fragment Caching or Partial Page Caching. You are free to do the same using Output Cache Substitution – a concept that provides you the facility to cache the output of a web page with some dynamic portions inside in page. Note that this is in sharp contrast to ASP.NET 1.x when we needed to implement the same as, in the partial page caching; “the overall page is dynamic, with cached regions in the middle”.
The following code snippet illustrates how we can implement output cache substitution using the <asp:substitution> control.
<asp:Substitution ID=”mySubstitutiionBlock” runat=”server” MethodName=”myMethod” />
Implement the method myMethod in such a way that it contains some code that would display static data. Now, the entire page would be cached except the contents of the <asp:substitution> control. Note that you can also achieve this programmatically using the Response.WriteSubstitution method.
 

Conclusion

 
Caching is a great tool that can be used to boost the performance web applications. This article has discussed the ways of storing data in the cache using ASP.NET with code examples wherever applicable. The next part in this series discusses storing data in the cache, i.e., Data Caching in ASP.NET.]]>

Leave a comment

Your email address will not be published.