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…