Caching In ASP.NET MVC

Caching is used to improve the performance in ASP.NET MVC. Caching is a technique which stores something in memory that is being used frequently to provide better performance. In ASP.NET MVC, OutputCache attribute is used for applying Caching. OutputCheching will store the output of a Controller in memory and if any other request comes for the same, it will return it from cache result.

Caching

Caching is used to improve the performance in ASP.NET MVC. Caching is a technique which stores something in memory that is being used frequently to provide better performance. In ASP.NET MVC, OutputCache attribute is used for applying Caching. OutputCheching will store the output of a Controller in memory and if any other request comes for the same, it will return it from cache result.

OutputCache attribute can have a parameter.

Duration

It describes the time in seconds.

The example of duration is given below.

  1. [OutputCache(Duration = 60)]  
  2. public ActionResult Index() {  
  3.     var emps = from e in db.Employees  
  4.     orderby e.ID  
  5.     select e;  
  6.     return View(emps);  
  7. }  
VaryByPara

It describes cache will be stored on the basis of a parameter. Cache will be stored on the basis of the list of semicolon separated by a string.

The example of VaryByParam is given below.
  1. [OutputCache(Duration = 60, VaryByParam = "Id")]  
  2. public ActionResult Index(int Id) {  
  3.     var emps = from e in db.Employees where e.DeptID = Id  
  4.     orderby e.ID  
  5.     select e;  
  6.     return View(emps);  
  7. }  
Location

It specifies where the cache is stored. Below is options available for locations
  • Any (Default)- Content is cached in three locations- the Web Server, any proxy Servers and the Web Browser.
  • Client- Content is cached on the Web Browser.
  • Server- Content is cached on the Web Server.
  • ServerAndClient- Content is cached on the Web Server and the Web Browser.
  • None- Content is not cached anywhere.
Now, the example of Location is given below.

  1. [OutputCache(Duration = 60, VaryByParam = "Id", , Location = OutputCacheLocation.Client)]  
  2. public ActionResult Index(int Id) {  
  3.     var emps = from e in db.Employees where e.DeptID = Id  
  4.     orderby e.ID  
  5.     select e;  
  6.     return View(emps);  
  7. }  

CacheProfile

CacheProfile is another way to handle cache. You can create profiles in web.config.

The example is given below.

In web.config

  1. <caching>  
  2.     <outputCacheSettings>  
  3.         <outputCacheProfiles>  
  4.             <add name="Long" duration="60" varyByParam="Id" />  
  5.             <add name="Medium" duration="60" varyByParam="none" />  
  6.             <add name="Short" duration="10" varyByParam="none" /> </outputCacheProfiles>  
  7.     </outputCacheSettings>  
  8. </caching>  

In Controller

  1. [OutputCache(CacheProfile = "Long")]  
  2. public ActionResult Index(int Id) {  
  3.     var emps = from e in db.Employees where e.DeptID = Id  
  4.     orderby e.ID  
  5.     select e;  
  6.     return View(emps);  
  7. }