Goal: Here, I will explain how to improve the performance of an ASP.Net MVC application using output caching.
Advantages
The following are the advantages:
-
It enables us to cache the content returned by a Controller action.
-
It stops the controller action from generating the same content each time.
-
We can set the caching parameter using the output cache.
-
We can specify where to send caching data. For example the Web Server or the user's browser and so on.
-
Caching enables us to avoid performing redundant work at the server.
How it helps
Imagine, for example, that our ASP.NET MVC application displays a list of database records in a view named "Display". Normally, each and every time that a user invokes the controller action that returns the Display view, the set of database records must be retrieved from the database by executing a database query.
If, on the other hand, we take advantage of the output cache then we can avoid executing a database query every time a user invokes the same controller action. The view can be retrieved from the cache instead of being regenerated from the controller action.
How to enable Output Caching?
namespace SampleMVC1.Controllers
{
public class customController:Controller
{
[OutputCache (Duration=20, VaryByParam="None" )]
public ActionResult display()
{
//Response.redirect();
// ViewData["Message"] = "Welcome to Dhananjay!";
return Content("I will Kill you MVC");
//return View();
}
}
}
Here, we are enabling Output cache for a controller named "customController". In the above listing the output of the display action will be cached for 20 seconds. In the code above, we are enabling the output cache for displaying the action of the customController controller. If we want to enable output caching for the entire controller (display), then we will need to write code as follows:
namespace SampleMVC1.Controllers
{
[OutputCache(Duration = 20, VaryByParam = "None")]
public class customController:Controller
{
// [OutputCache (Duration=20, VaryByParam="None" )]
public ActionResult display()
{
//Response.redirect();
// ViewData["Message"] = "Welcome to Dhananjay!";
return Content("I will Kill you MVC");
//return View();
}
}
}
How to calculate caching duration
If we want to cache the output of a controller action for one day then the parameter to specify the duration would be calculated as 60 sec * 60 Min * 24 hrs = 86400 seconds.
[OutputCache(Duration = 86400, VaryByParam = "None")]
Where Content is Cached
By default, when we use the "[OutputCache]" attribute, the content is cached in the following three locations:
-
the web server
-
any proxy servers
-
the web browser.
We can control exactly where the content is cached by modifying the Location property of the "[OutputCache]" attribute.
We can set the Location property to any one of the following values:
-
Any
-
Client
-
Downstream
-
Server
-
None
-
ServerAndClient
By default, the Location property has the value "Any". However, there are situations in which you might want to cache only on the browser or only on the server. For example, if we are caching information that is personalized for each user then we should not cache the information on the server. If we are displaying different information to different users then we should cache the information only on the client.
How to create a cache profile
A cache profile can be created in a Web.config file. Creating a cache profile in the Web.config file has many advantages, like:
-
We can store controller action cache in one central location.
-
We can create one cache profile and apply the profile to several controllers or controller actions.
-
We can modify Web.config file without recompiling the application.
Cache profile in Web.config file:
<caching>
<outputCacheSetting>
<outputCacheProfile>
<add name="CacheHour" duration="3600" />
</outputCacheProfile>
</outputCacheSetting>
</caching>
The following shows how to apply a cache profile in a Controller action:
[OutputCache(Duration = 10, VaryByParam = "none")]
public ActionResult Index()
{
return View();
}
Caching Example
This caching example will display the same time for 10 seconds.
Home\Index.Cs
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
[HandleError]
public class HomeController : Controller
{
[OutputCache(Duration = 10, VaryByParam = "none")]
public ActionResult Index()
{
return View();
}
}
}
View\Index.CS
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="MvcApplication1.Views.Home.Index" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Index</title>
</head>
<body>
<div>
The current time is: <%= DateTime.Now.ToString("T") %>
</div>
</body> </html>