Donut Hole Caching is the inverse of Donut cache. As the previous statement, the Donut Caching is used to cache a maximum portion of the view/page, but Donut Hole Caching is used to cache only small portion of the view/page. More about Donut Caching refer here . Donut Hole caching procure by OutputCache and ChildActionOnly attribute.

What is in Mind?

Q1 : What is different type of caching in MVC
We can use OutputCache, DonutCache and Donut Hole Cache to cache the content.

Q2 : When can we implement?
OutputCache – Use when need to cache Whole page/View
DonutCache – Use to cache Whole page expect small portion of it.
Donut Hole Cache – Need to cache small portion instead of caching whole page

When to use ?

Let consider the scenario, having a website which contains the home page with dynamic content(maximum of the page) which load the content based on the user login. And also it has News feed section which contain small portion of the page(static content for all user).

In this scenario, we need to cache the common portion of the page for all the users. So here we cache News feed section instead of dynamic content.

How to Implement ?

In ASP.NET MVC, we can cache whole action with OutputCache Attribute. In mean while if we do not want to cache the entire action, we just want to cache the small portion of page/view. Then this can be achieved by using combination of ChildActionExtensions and OutputCache Attribute.

Controller:

From the below code, the LoadNewsFeed content (ie., loading partial view) in the Index view has cached for 60 seconds for all users expect static content which is specified inside the view.The ChildActionOnly attribute ensures that an action method can be called only as a child method from within a view. In simple, ChildActionOnly attribute are treated as non-action method (It treated as normal method). More about ChildActionOnly

View

Creating a view with displaying current time and render LoadNewsFeed partial view in it.

Output:

Run the application, when we browse DonutCache controller then it will load the index view. If we refresh the page before 60 seconds, then the index view will refresh because it has not cached . But LoadNewsFeed has not refreshed every time because it has been cached by using Donut Hole caching.

donut Hole caching- in MVC dotnet-helpers

 

Keep Happy coding…