Caching in ASP.NET MVC

Caching can improve the application performance in web applications. In this article we will see how to implement output caching in ASP.NET MVC.

Caching can improve performance in web applications by storing the content generated by the server. The generated content can be stored either on the client or the server. ASP.NET MVC action methods return content each time they are invoked. This content needs to be generated and can come from a database or files or generated using some other resources. So generating the content can take server time and also consume server resources. So if we can avoid this content generation each time an action method is invoked this can save server time and resources that could be used to serve other requests.

Caching.jpg
                                            Fetching items from the cache

The diagram above shows how output is fetched from the cache when the action method is called as a result of a request made by the client. If the output is in the cache it is returned from the cache otherwise fetched from the database and stored in the cache.

In ASP.NET MVC we can specify where we want to cache the content of the action method. To use caching in the action method we use the OutputCache attribute and specify the Duration and VaryByParam arguments.
The following code will cache the contents of the action method for 1 minute.

 

  1. [OutputCache(Duration = 60, VaryByParam = "none")]  
  2. public ActionResult Login(string name)

We can specify a longer duration for the cache by specifying a greater value for the duration parameter. For example if we want to store the output for 1 hour we can specify Duration as 3600(60*60). We can specify where we want to store the cached content. We can store on the client, the server or any other location. The following code is used to specify the cache location as client.

[OutputCache(Duration = 60, VaryByParam = "none", Location = OutputCacheLocation.Client)]

The various values we can specify for the location parameter are:

  • Any

  • Client

  • Downstream

  • Server

  • None

  • ServerAndClient

The default is "Any". Note that we can specify the OutputCache attribute either on the action method or the controller class itself. When specified on the controller class it will cause the content of all the action methods to be chached using the settings specified in the OutputCache attribute.

The code above will store the same copy of the content irrespective of the querystring parameters. We can specify that we want to store various versions of the content based on various query strings by specifying the VaryByParam value.The following code will store various versions of the content based on the query string parameter "Name".

[OutputCache(Duration = 60,Location = OutputCacheLocation.Client,VaryByParam="Name")]

Instead of specifying the cache properties as above we can configure the cache properties once in the web.config file using the cache profile element and apply it to multiple action methods. The following cache profile configuration setting in the web.config causes the action method to store the content for 1 minute.

  1. <caching>  
  2.     <outputCacheSettings>  
  3.       <outputCacheProfiles>  
  4.         <add name="TestCache" duration="60" varyByParam="none"/>  
  5.       </outputCacheProfiles>  
  6.     </outputCacheSettings>  
  7.   </caching> 

We can specify the cache profile above to the action method as:

[OutputCache(CacheProfile = "TestCache")]

The advantage of using the cache profile is that we are not required to specify the OutputCache parameters everytime.

So if the controller uses some complex or heavy logic then using the OutputCaching can improve the performance drastically.


Similar Articles