Fragment Caching - Caching parts of a Page

Caching is one of the recommended techniques to improve the performance of web applications by avoiding repeated requests to the server.

It serves the purpose for faster response as well as reducing the load to the Server.

Output caching can be enabled easily by just including the following code in the Top of the aspx page

<%@ OutputCache Duration="1200" VaryByParam="None" %>

This would result in the whole page being cached.

However, there will be situations where we would like to cache only sections of page rather than the whole page. There may be sections of the page where the information gets updated every second or so and these sections should not cached.

This can be implemented in ASP.NET using Fragment Caching. Fragment Caching is the technique by which we can cache a sections of the page instead of caching the whole page.

This is accomplished by dividing the pages into usercontrols and caching the individual usercontrols.

Say we have a page which has 5 sections. We can split the static sections into usercontrols and cache them such that only that portion of the page is cached..

The syntax for fragment caching is similar to output caching

<%@ OutputCache Duration="1200" VaryByControl="None" %>

We can therefore cache only portions of the page and avoid the whole page being cached.

Note the VaryByControl attribute. It is unique to user controls caching and we can specify the ID of a webcontrol (ex. DropDownList) to invalidate the cache if the control's selection / property is changed.

Caching has been improved a lot with the .NET 2.0 version which is scheduled to be released sometime during early 2005.

Print | posted on Monday, April 25, 2005 7:53 AM

Comments on this post

# re: Fragment Caching - Caching parts of a Page

Requesting Gravatar...
thanks that helped a lot
Left by sparsha on Aug 23, 2005 2:55 PM

# re: Fragment Caching - Caching parts of a Page

Requesting Gravatar...
Gr8 info...
Left by Sam on Dec 27, 2007 9:24 PM

# re: Fragment Caching - Caching parts of a Page

Requesting Gravatar...
when i cashe user user control. click even on user control will not work, for example menu conterol for master page. if create user control for menu and cashe that usercontrol. click event will not work till it expires duration
Left by rashmi on Sep 24, 2008 6:33 PM

# re: Fragment Caching - Caching parts of a Page

Requesting Gravatar...
sdsa ds
Left by aji on Aug 03, 2009 4:48 PM

# re: Fragment Caching - Caching parts of a Page

Requesting Gravatar...
In the first place, I don't know what is the importance of caching. Is this the same with storing? I am not quite sure though. - Dean Spasser
Left by Dean Spasser on Jan 05, 2012 7:37 AM

Your comment:

 (will show your gravatar)