Output Caching in MVC 4 Explained

Hi,

In the below post we will learn about the different options for output caching for MVC 4. First of all let me explain you that why caching is required? why should we think on it ? . For it, support your application have a profile page where you have a dropdown of cities. you have stored the list of cities on in the database based on the state and states are based on the country. so whenever this page requested , your code generates the list from the database and sends back to the requested client. now just think that is the cities names are frequently changing in the world. off course not, right.so why this operation is happening again and again . we not we just fetch the list once and then serve it afterwards.

So this is the exact place where you need the caching. now we will see what are the options are provided for it in MVC framework.

The MVC provided an  Action filter attribute named “OutputCacheAttribute” which helps you for it. for example

[OutputCache(Duration = 180, VaryByParam = "none")]

Just adding the above line of code above an action method as below will cache the action output for 180 seconds.

[OutputCache(Duration = 180, VaryByParam = "none")]
public ActionResult About()
{
ViewBag.Message = "Your app description page.";

return View();
}

you can provide different parameters for “OutputCache”. In the below image you can see the list of supported parameters for “OutputCache”.

image

  • CacheProfile  : This parameter sets a custom cache profile. you can create a Cache Profile in web.config which can be added into the “” section of it . Creating cache profile is good way to manage the caching behavior from a central location. whenever you want to modify it , you just need to change the web.config file, no recompilation of the application required. for example see the below web.config section .




<add name="MyCustomProfile" duration="1800" enabled="true" varyByParam="none" />


In the above lines I have defined an output Cache named “MyCustomProfile” and added some properties of it. after applying it to the actions it the content will be cached for 1800 seconds. you can apply the it as below.

[OutputCache(CacheProfile = "MyCustomProfile")]
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}

so whenever you want to change the cache properties , you just need to change the web.config file except you want to change the profile name . 🙂

 

  • Duration :This parameter specify the duration of caching in seconds , for example the below setting will cache the output for 180 seconds.

[OutputCache(Duration=180)]
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}

  • Location : This parameter specify the location where you want to cache the  output , In the below picture you can see that it support six options The default is “Any”.

image 

    • OutputCacheLocation.Any : This option tells that the output can be cached on the original client browser or proxy server between the requests or the original server where it is processed.
    • OutputCacheLocation.Client: This options tells that the output can be cached on the original client browser.
    • OutputCacheLocation.Downstream : This options tells that the output can be cached on any caching capable device which participate between the request and response cycle except the server which processed the request .
    • OutputCacheLocation.None:Its simply means cache is disabled .
    • OutputCacheLocation.Server : This options tells that the output can be cached on the original server where it processed .
    • OutputCacheLocation.ServerAndClient : This options means that the output can only be cached on the original server or original client , not on any proxies between the cycle .

  • NoStore : This option tells that whether the cache should be stored or not. This is Boolean option which returns true if the “Cache-Control: no-store” directive is set , otherwise returns false ,the default value is false.

[OutputCache(Duration=60,NoStore=true)]

  • SqlDependency : This option takes the the set the database names and tables names pair and then determine the caching based on it.To make it working you need to enable it first , first you need to add the below section the web.config file in section.


<sqlCacheDependency enabled="true" pollTime="6000">

<add name="Northwind" connectionStringName="NorthwindConnectionString"/>


In the above sample the “NorthwindConnectionString” is the connection string name and the “Northwind” is the name of database . now the next step is to enable it using the command prompt  you need to locate the windows command prompt to “%windir%\Microsoft.NET\Framework\v4.0.30319\” and then type the below command to enable it.

 

aspnet_regsql.exe -S [YOURSERVER] -U [USERNAME] -P [PASSWORD] -ed -d [DATABASE] -et -t [TABLENAME]

 

For example I have enabled it for product table of Northwind database as below .

image
now you can add it on any action as above.

[OutputCache( Duration=60, SqlDependency="Northwind:Products")]
public ActionResult About()
{
ViewBag.LastRenderTime = DateTime.Now;
ViewBag.Message = "Your app description page.";
return View();
}

  • VaryByContentEncoding : In this option we can specify the list of  character set(like gzip, deflate  ) seperated by semicolon as below.

[OutputCache(Duration = 60, VaryByContentEncoding = "gzip;deflate")]
public ActionResult About()
{
ViewBag.LastRenderTime = DateTime.Now;
ViewBag.Message = "Your app description page.";
return View();
}

when you verify the header information in any web debugger tools like fiddler then you can see that it added the vary attribute for caching as below.

image

  • VaryByCustom: This parameter is helpful if you want to cache the output based on some custom string. support you want to cache based on logged in user name then you can use this parameter  as below .

[OutputCache(Duration = 60, VaryByCustom = "UserRoleList")]
public ActionResult About()
{
ViewBag.LastRenderTime = DateTime.Now;
ViewBag.Message = "Your app description page.";
return View();
}

In the above code we have used “UserRoleList” as custom parameter .Then we need to override the “GetVaryByCustomString” method in the Global.asax file as below.

public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom.ToLower() == "userrolelist")
{
string[] userRoles = System.Web.Security.Roles.GetRolesForUser(User.Identity.Name);
if (userRoles.Count() > 0)
{
return String.Join(";", userRoles.ToArray());
}
else
{
return "User";
}
}
return base.GetVaryByCustomString(context, custom);
}

The above method is checking whether the request is for “UserRoleList” parameter , if so, then it is returning a “;” seperated list of user role if user belongs to any role otherwise returning “User” string

  • VaryByHeader : This option works on a list of valid http headers, different version get cached based on http header parameter setting. for example in the below code different versions of about view get cached based on the “Accept-Language” information of Http Headers.

[OutputCache(Duration = 60, VaryByHeader = "Accept-Language")]
public ActionResult About()
{
ViewBag.LastRenderTime = DateTime.Now;
ViewBag.Message = "Your app description page.";
return View();
}

  • VaryByParam : This can take multiple parameters and the value can be “none” , “*” or any valid query string or post parameter name . for example in the below code the method GetProductDetails takes 3 parameters and do some query based on it. So, if you want your cache based on productType and productMinCost then below can be valid settings for VaryByParam parameter

[OutputCache(Duration=60,VaryByParam="productType;productMinCost")]
public ActionResult GetProductDetails(string productType, decimal productMinCost, int stockQuantity)
{
//Code of this function
}

 

Hope you find this post useful, Thanks for reading . 🙂
Advertisements

4 thoughts on “Output Caching in MVC 4 Explained

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s