Please enable Javascript to correctly display the contents on Dot Net Tricks!

Donut Caching and Donut Hole Caching with Asp.Net MVC 4

Posted By : Shailendra Chauhan, 21 Jan 2013
Updated On : 22 Jan 2013
Total Views : 134,509   
Support : MVC4 & MVC3

Donut caching is the best way to cache an entire web page except for one or more parts of the web page. Before Donut caching, we have Output Caching which cache the entire web page.

When to use Donut caching...

Suppose, you have a web application in which some pages like HomePage,Tools etc. are same for all the users excepts the user's logged in details like username.

If you want to cache all these pages for all the users by using OutputCache with VaryByParam UserID, then the entire page would be cached every time for each user with a different user name (or whatever your dynamic part of the page is). This is not a good practice since there will be 1000 cached pages if there are 1000 logged in user at a time.

To resolve this issue, Donut Caching was introduced which cached only one copy of the entire page for all the user except for a small part which remain dynamic. This small part act like as a hole in the cached content and much like a donut.

Donut caching is very useful in the scenarios where most of the elements in your page are rarely changed except the few sections that dynamically change, or changed based on a request parameter.

The MvcDonutCaching NuGet Package

For implementing Donut caching you need to install MvcDonutCaching NuGet package within your Visual Studio 2012. You can install this through NuGet or directly type the command "install-package MvcDonutCaching" in the "Package Manager Console" as shown below:

Once the MvcDonutCaching NuGet package is installed, you can add the DonutOutputCache attribute to the action methods or to the controller. Most of the options of OutputCache attribute are also available with Donut Caching.

public ActionResult Index()
public ActionResult Index()
[DonutOutputCache(Duration=60, VaryByCustom="whatever")]
public ActionResult Index()
[DonutOutputCache(Duration=60, VaryByParam="something;that")]
public ActionResult Index(string something)
[DonutOutputCache(Duration=60, VaryByParam="none")]
public ActionResult Index(int referrerId)

Donut Hole Caching

Donut Hole Caching is the inverse of Donut caching means while caching the entire page it cached only a small part of the page(the donut hole).

When to use Donut Hole caching...

Suppose, you have a web application in which ProductCategory is shown on each and every pages so it makes sense to render all of the categories just once and cache the resulting HTML by using Donut Hole Caching.

Donut Hole caching is very useful in the scenarios where most of the elements in your page are dynamic except the few sections that rarely change, or changed based on a request parameter. Asp.Net MVC has great support for Donut Hole caching through the use of Child Actions.

public ActionResult CategoriesList()
 // Get categories list from the database and
 // pass it to the child view 
 ViewBag.Categories = Model.GetCategories();
 return View();

View with Donut Hole Caching

Now call the above action method "CategoriesList" from the parent view as shown below:

<h1>MVC Donut Hole Caching Demo</h1>


<!-- Rest of the page with non cacheable content goes here -->
What do you think?

I hope you will enjoy the tips while programming with MVC Razor. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Recommended for you
About the Author
Shailendra Chauhan

Shailendra Chauhan is an Entrepreneur, Author, Architect, and Corporate Trainer. He has rewarded as Microsoft MVP for his exceptional contributions in Microsoft Visual Studio and Development Technologies.

With more than 7 years in hand experience Shailendra Chauhan is a polymath in the domains of Microsoft .NET technologies and an array of other technologies including JavaScript, AngularJS, Node.js, Ionic and NoSQL Databases to name but a few.

He is the author of some of most popular e-books which encompass technical Interview on Node.js Interview Questions and Answers , ASP.NET MVC Interview Questions and Answers , AngularJS Interview Questions and Answers and LINQ Interview Questions and Answers. Furthermore he is a technical reviewer for book on ASP.NET MVC 4 Mobile App Development. Know more...
Free Interview Books
10 SEP
ASP.NET MVC with AngularJS Development (offline)

SAT,SUN 05:00 PM-06:30 PM IST

More Details
ASP.NET MVC with AngularJS Development (online)

Weekdays (Mon-Fri) 07:30 AM-09:00 AM IST

More Details
ASP.NET MVC with AngularJS Development (online)

Weekend (Sat, Sun) 03:00 PM-05:00 PM IST

More Details
29 AUG
NodeJS Development (online)

MON-FRI 06:00 AM-07:30 AM IST

More Details
27 AUG
PPC Marketing (offline)

Sat, Sun 03:00 PM-05:00 PM IST

More Details
20 AUG
NodeJS Development (offline)

Sat, Sun     11:00 AM-12:30 PM IST

16 AUG
NodeJS Development (online)

Mon-Fri     09:00 PM-10:30 PM IST

12 AUG
ASP.NET MVC with AngularJS Development (online)

Mon-Fri     09:30 PM-11:00 PM IST

AngularJS Development (online)

Mon-Fri     08:00 PM-09:30 PM IST

ASP.NET MVC with AngularJS Development (online)

Mon-Fri     07:30 AM-09:00 AM IST

24 JUL
AngularJS Development (offline)

Sat,Sun     08:00 AM-09:30 AM

24 JUL
ASP.NET MVC with AngularJS Development (offline)

Sat, Sun     09:30 AM-11:00 AM

14 JUL
PPC Marketing (offline)

Sat, Sun     12:00 AM-02:00 PM IST

11 MAY
.NET Development (offline)

Mon-Fri     9:00 AM-11:00 AM IST