Different Kinds of Caching in asp.net MVC4

Asp.Net MVC
Friday, July 05, 2013

When your web site has so many users it means your server has so many recieved requests, there is no need to query for every request Made by users because it will make some performance issues, for example there are some static content in your web site that you don’t update them for several months, in these cases you can cache those content and don't force your server to get them from data base in every request. there are different kinds of caching in asp.net 

OutPutChaching: ASP.net has a build-in caching mechanism called OutputCache, it's too easy to use.

  1.         [OutputCache(Duration = 6000)]
  2.         public ActionResult ActionResult()
  3.         {
  4.             return View();
  5.         }

This means you cache this action for 6000 seconds in your website's server. Also you can create different cache for different values passed to the action by using VaryByParam:

  1.         [OutputCache(Duration = 6000, VaryByParam = "id")]
  2.         public ActionResult Action2(int id)
  3.         {
  4.             return View();
  5.         }

In this case you cache Action2 for 6000 second for parameter named "id", and also it's possible to cache the action for every parameter

  1.         [OutputCache(Duration = 6000, VaryByParam = "*")]
  2.         public ActionResult action3(int Id)
  3.         {
  4.             return View();
  5.         }

You can also declare the location of the caching within this attribute, take a look

  1.         [OutputCache(Duration = 6000, VaryByParam = "*", Location = OutputCacheLocation.Client, NoStore = true)]
  2.         public ActionResult action4(string Name, int id)
  3.         {
  4.             return View();
  5.         }

This means you are caching the action to client.

OutputCacheProfiles Is an interesting  feature that you can define your caching plans in Web.Config and then use it in you controller or actions, like this

  1. <caching>
  2. <outputCacheSettings>
  3.     <outputCacheProfiles>
  4.         <add name="TwoHours" duration="7200" varyByParam="none"/>
  5.         <add name="OneDay" duration="86400" varyByParam="none"/>
  6.         <add name="OneWeek" duration="604800" varyByParam="none"/>
  7.     </outputCacheProfiles>
  8. </outputCacheSettings>
  9. </caching>

After that you can use your plans, for example

  1.         [OutputCache(CacheProfile = "OneDay")]
  2.         public ActionResult action5()
  3.         {
  4.             return View();
  5.         }
  1.         [OutputCache(CacheProfile = "OneWeek")]
  2.         public ActionResult action6()
  3.         {
  4.             return view();
  5.         }

Donut caching: if you want to cache the entire of page except one or a few parts, donut caching is the best choice. For example you have a static page which has a dynamic part (partial view) you can use donut caching in this case, unfortunately! There is no any built in mechanism for donut caching like outoutcaching,then you have get the package by Nuget 

donut caching

And then call use the DonutOutputCache in an action like this

  1.        [DonutOutputCache(Duration = 6000)]
  2.         public ActionResult action8(int id)
  3.         {
  4.             return View();
  5.         }

That's it, you have defined donut caching for this action, now when you call this action in your View you can make donut caching off or on , like this :

  1. @html.action("action8","SampleController",true)   

This means that action8 is not include the donut cash, if you change the true to false it will be cached! I'd prefer to use donut caching instead of caching multiple actions by outputcahing if I want to cache the whole page. BTW You can also use cache profile in Donut caching too.

Donut Hole caching: donut caching is about caching the entire page but a little part, now donut hole caching is about cashing a part of a dynamic page like tags of this blog that you can see in the right side(at the time of writing this post!) , fortunately! asp.net MVC4 has built in mechanism for donut hole caching and you don't need to use any external package. I've cached the tags of this blog like this:

  1.         [ChildActionOnly]
  2.         [OutputCache(Duration = 600)]
  3.         public PartialViewResult TagSidebar()
  4.         {
  5.             var tags = _tagRepository.GetAllTag();
  6.             return PartialView("_TagsSidebar", tags);
  7.         }

You can call PartialViewResult only inside a View and it's not accessible from URL, when you use ChildActionOnly attribute you implement the Donut Hole caching in the background too!

Caching webApi in MVC4 I searched about that and it seems there is no any way to cache WebAPI in MVC4 and you have to write your own custom caching, this article could be helpful!