.Net, Programming

Improve Performance By Caching In MVC

In this article, you learn to implement caching and improve the performance of your MVC website. You can get the very impressive behavior of your site and faster than normally execute each time with lots of users request to your site.

Generally, when the user requests an action to view all records from the particular table and just display as table and that is your landing page which is being used by all users who will first come to your website. So every time a new user comes and request that page and action will be called and records data has to be loaded from the database every time and that will affect your performance to get data and render. So to reduce the load on the server to get the data and load we can cache that action output and store for a given time and give it to every request to reduce database calling and model creation logic. To use such a caching mechanism in ASP.Net MVC, just add [OutputCache] attribute to an individual action or an entire controller class.

using System.Web.Mvc;

namespace MvcApplication.Controllers
{
public class TestController : Controller
{
[OutputCache(Duration=30, VaryByParam="none")]
public ActionResult Index()
{
return DateTime.Now.ToString("T");
}

}
}

Use OutputCache attribute on the controller in above code can cache the Index action result of Test Controller so while you access www.yourwebsitename.come/Test or www.yourwebsitename.com/Test/Index then you can get the same result in 30 seconds. After 30 seconds, the cache will automatically reset and get a new result of action result. If you create a view and display the output of this action, then the returned time will be same till 30 seconds and after 30 seconds you will get latest time and cache result updated automatically.

OutputCache attribute can be used many ways with different parameters and some of the basic as below:

Duration
Give the number of seconds to retain the output of the action as a cached output.

Location
There are different locations where you can cache your result. Locations generally on the browser client (where the request originated), on the server (where the request was processed), on a proxy server (or any server) participating in the request. The location can be defined by OutputCacheLocation Enumeration and members defined by any one of the following values:

  • Any – This is a default option.
  • Client – Generally the browser client.
  • Downstream – proxy or other servers.
  • None – disable cache for the requested page.
  • Server – cache on the web server.
  • ServerAndClient – cache on the origin server or the requesting client.

To define the location in the attribute you have to provide value from OutputCachedLocation Enumeration:

using System.Web.Mvc;

namespace MvcApplication.Controllers
{
public class TestController : Controller
{
[OutputCache(Duration=30, VaryByParam="none", Location=OutputCacheLocation.Client)]
public ActionResult Index()
{
return DateTime.Now.ToString("T");
}

}
}

VaryByParam
To Varying, the output cache works with request parameter that you have defined so when a request for a particular value like an id to get data for that id and action look into the database on every request, but if you request with the same id in route parameter then it will give you cached output. The possible values include:

None – Don’t vary cache by parameters
* – Vary cache in every case
Querystring or POST Parameter name – Vary cache by Querystring or Post values.

To define VaryByParam in OutputCache as following the way to just provide values.

using System.Web.Mvc;

namespace MvcApplication.Controllers
{
public class TestController : Controller
{
[OutputCache(Duration=30, VaryByParam="id", Location=OutputCacheLocation.Client)]
public ActionResult Index()
{
return DateTime.Now.ToString("T");
}

}
}

For example, In some case, you would like to create one master data to display information about customers and related detail section for selected customer display just below to the master in the page. To improve such a kind of page you have to provide value in VaryByParam for master page with “None” so don’t depend on any query string or post parameter and just retain cached output and just worry about every detail section that will be requested by user and just pass “id” as routing parameter so every time master don’t fetch data from database and just detail section with definition VeryByParam=”id” will fetch data from database.

So you can define various parameter values and customize your cache mechanism. In above scenario, you have to define every parameter in the attribute of OutputCache for every action or controller, but an alternate option available to reduce such efforts by using a Cache Profile.

You can create cache profile in the web.config file and by defining a cache profile you get the benefit to just provide a name of a cache profile and setting will be applied to actions or controllers and also you can off caching to do changes in web.config file without recompiling your application.

For example, you can define “testprofile” in web configuration section within the section of a web.config file.

<system.web>
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="testprofile" duration="1000" varyByParam="none"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
</system.web>

Now you can use that profile name “testprofile” in OutputCache attribute

using System.Web.Mvc;

namespace MvcApplication.Controllers
{
public class TestController : Controller
{
[OutputCache(CacheProfile="testprofile")]
public ActionResult Index()
{
return DateTime.Now.ToString("T");
}

}
}

Output caching is very useful to improve your MVC website dramatically and get fast output and reduce the load on the server.

Tagged

Leave a Reply

Your email address will not be published. Required fields are marked *