Q1 : What is different type of caching in MVC Q2 : When can we implement? 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. 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. 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
Creating aย view with displaying current time and render LoadNewsFeedย partial view in it.
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. Keep Happy coding… What is in Mind?
We can use OutputCache, DonutCache and Donut Hole Cache to cache the content.
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 pageWhen to use ?
How to Implementย ?
Controller:
View
Output:
Donut Hole Caching in ASP.NET MVC
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class DonutCacheController : Controller
{
public ActionResult Index()
{
ViewBag.CurrentDataTimeMessage = DateTime.Now.ToString();
return View();
}
[/fusion_builder_column][fusion_builder_column type="1_1" background_position="left top" background_color="" border_size="" border_color="" border_style="solid" spacing="yes" background_image="" background_repeat="no-repeat" padding="" margin_top="0px" margin_bottom="0px" class="" id="" animation_type="" animation_speed="0.3" animation_direction="left" hide_on_mobile="no" center_content="no" min_height="none"][ChildActionOnly]
[OutputCache(Duration = 60)]
public ActionResult LoadNewsFeed()
{
ViewBag.CurrentDataTimeMessage = DateTime.Now.ToString();
return View();
}
}
1
2
3
4
5
<h2>Index</h2>
<h3>Message from DonutCache -> Index </h3>
<h3>@ViewBag.CurrentDataTimeMessage</h3>
@Html.Action("LoadNewsFeed")
Leave A Comment