Ehsan Ghanbari

Experience, DotNet, Solutions

Different Kinds of Caching in MVC4

When your web site has so many users it means your server has so many received 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 website 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 database in every request. there are different kinds of caching in 


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


        [OutputCache(Duration = 6000)]
        public ActionResult ActionResult()
            return View();


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


        [OutputCache(Duration = 6000, VaryByParam = "id")]
        public ActionResult Action2(int id)
            return View();


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


        [OutputCache(Duration = 6000, VaryByParam = "*")]
        public ActionResult action3(int Id)
            return View();


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


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


This means you are caching the action to the client.

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


        <add name="TwoHours" duration="7200" varyByParam="none"/>
        <add name="OneDay" duration="86400" varyByParam="none"/>
        <add name="OneWeek" duration="604800" varyByParam="none"/>


After that, you can use your plans, for example


        [OutputCache(CacheProfile = "OneDay")]
        public ActionResult action5()
            return View();
        [OutputCache(CacheProfile = "OneWeek")]
        public ActionResult action6()
            return view();

Donut caching: if you want to cache the entire of the page except for 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 to get the package by Nuget 

donut caching

And then call use the DonutOutputCache in an action like this


       [DonutOutputCache(Duration = 6000)]
        public ActionResult action8(int id)
            return View();


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 :




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! 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:


        [OutputCache(Duration = 600)]
        public PartialViewResult TagSidebar()
            var tags = _tagRepository.GetAllTag();
            return PartialView("_TagsSidebar", tags);


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!

About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns and I'm spending a lot of my time on architecture subject. Since 2008, I've been as a developer for companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the&nb Read More

Post Tags
Pending Blog Posts
Strategic design
Factory Pattern
time out pattern in ajax
Selectors in Jquery
Peridic pattern
How to query over Icollection<> of a type with linq
How to use PagedList In MVC
Domain driven design VS model driven architecture
What's the DDD-lite?
Using Generic type for type casting in F#