What is Donut Caching?

03 November 2021 at 10:00 by ParTech Media - Post a comment

In our previous blog on caching, we discussed the importance of caching, types of cache, and how it will have an impact on the performance of an application. Different types of caching require different techniques to implement. The complexity also varies.

For example, in the data caching technique, we need to be aware of the collection that needs to be cached and the representational keyword for it (for retrieval). With these two parameters known, it is easy to cache the data in the memory, retrieve it and use it later.

We also saw that web caching is possible in ASP .Net. However, the complexity in implementing output caching is more than data caching. Output caching is the concept of caching the output of a controller’s action and returning the cached result.

There are a lot of web applications with a lot of utilities being created and deployed. Consider an application that requires authentication to see further content on the webpage. And after a successful login, the website displays a common webpage with the user name and a few other profile details.

Other than the user's info, the rest of the content of the webpage remains the same. In this case, the caching can be done for the common data that is present on the page, which is anything but a straightforward implementation of caching.

So, while performing caching, we need to consider a few and leave a few. And this type of caching resembles the structure of donuts and hence is named after it. The portion of the page that is being cached is the solid part of the donut while the hole is the left-out page of the web application.

In this post, we will see in detail about donut caching in ASP .Net MVC and how to implement it.

Table of contents

  1. What is Donut caching?
  2. How is Donut caching achieved?
  3. How to implement Donut caching?
  4. Conclusion

What is Donut caching?

Donut caching falls under the output caching technique, where the caching can be achieved partially for the required components in the web page.

Full-page caching, as the name suggests, is caching the entire page. It is achieved using the OutputCache attribute available in ASP .Net MVC, and this option has been available since the beginning of .NET MVC.

The concept of partial caching, which in other words is called Donut caching, was made available only in the ASP .Net MVC 3 version. The concept of caching the page element-wise is called Donut Hole Caching, whereas caching the entire page by leaving out a few elements is called Donut caching.

How is donut caching achieved?

Donut caching is achieved using the action filter attribute which can be applied at the controller level or action level. When the action method is executed for the first time, the OutputCache attribute intercepts the action result before it is sent to the client. And this output is configurable for a particular period.

On calling the same action method again, the data will be returned from the cache. Before the output is returned, the configuration has to be done on the elements that need to be cached and that need not be cached. The partial caching is achieved using the MemoryCache introduced in the .Net framework 4.0 version.

How to implement donut caching?

To implement donut caching, we are going to create an ASP .Net MVC web application, which loads with the default View, Controller, and Model. The default project structure looks like the below -

The idea is to cache one part of the website. At the same time, the remaining part of the page will be fetched dynamically when there is a request from the user’s side.

To achieve this, the Index.cshtml file under the home folder will be modified to contain a text and a date-time which is assigned in the controller using the ViewBag.

Along with that, another page, “LoadDetails” will be created in the same Home folder of the view. In this, there will be a second text denoting the place from which it is displayed and along with another date-time data.

In the Index.cshtml, the LoadDetails page will be called.

By right-clicking the project in the Manage NuGet options, install the MvcDonutCache package.

Once installed, from the HomeController, Index Method, DonutOutputCache attribute will be set with the respective duration of caching.

Code in Index.cshtml

<h2>PARTECH - Index</h2>

<h2>Message from HomeController -> Index </h2>


@Html.Action("LoadDetail", "Home", new { name = "test" }, true)

Code in LoadDetail.cshtml

<h2> This Message is from Partial View - LoadDetail</h2>



Code in HomeController.cs

 [DonutOutputCache(Duration = 60)]

​    public ActionResult Index()

​    {

​      ViewBag.CurrentDataTimeMessage = DateTime.UtcNow.ToString();

​      return View();

​    }


​    public ActionResult LoadDetail()

​    {

​      ViewBag.CurrentDataTime = DateTime.UtcNow.ToString();

​      return View();

​    }

With the help of above codes in respective files, if we run the solution, the below output will be displayed initially.

And if we refresh after a few seconds, the first timestamp, the one that is being printed from the Index file will not change. However, the timestamp printed from the LoadDetail will keep changing as we refresh.

And the timestamp Index will change after the provided cache duration is over.

Similarly, the required caching functionality can be achieved as per different needs. If the attribute OutputCache is implemented instead of the DonutCache, then the time in both Index and LoadDetail gets cached. DonutCacheOption also provides other options with the help of values specified to VaryByParam and VaryByCustom.

Final Words

Donut caching provides the option to cache the MVC controller’s action method result and helps in achieving better application performance. So go ahead and use it in your next application and enjoy improved performance.