Please enable Javascript to correctly display the contents on Dot Net Tricks!

Understanding Caching in Asp.Net MVC with example

  Author : Shailendra Chauhan
Posted On : 05 Jan 2013
Total Views : 196,476   
Updated On : 26 Sep 2016

Caching is a most important aspect of high-performance web application. Caching provides a way of storing frequently accessed data and reusing that data. Practically, this is an effective way for improving web application’s performance.

Advantage of Caching

  1. Reduce hosting server round-trips

    When content is cached at the client or in proxies, it cause minimum request to server.

  2. Reduce database server round-trips

    When content is cached at the web server, it can eliminate the database request.

  3. Reduce network traffic

    When content is cached at the client side, it it also reduce the network traffic.

  4. Avoid time-consumption for regenerating reusable content

    When reusable content is cached, it avoid the time consumption for regenerating reusable content.

  5. Improve performance

    Since cached content reduce round-trips, network traffic and avoid time consumption for regenerating reusable content which cause a boost in the performance.

Key points about Caching

  1. Use caching for contents that are accessed frequently.

  2. Avoid caching for contents that are unique per user.

  3. Avoid caching for contents that are accessed infrequently/rarely.

  4. Use the VaryByCustom function to cache multiple versions of a page based on customization aspects of the request such as cookies, role, theme, browser, and so on.

  5. For efficient caching use 64-bit version of Windows Server and SQL Server.

  6. For database caching make sure your database server has sufficient RAM otherwise, it may degrade the performance.

  7. For caching of dynamic contents that change frequently, define a short cache–expiration time rather than disabling caching.

Output Cache Filter

The OutputCache filter allow you to cache the data that is output of an action method. By default, this attribute filter cache the data till 60 seconds. After 60 sec, if a call was made to this action then ASP.NET MVC will cache the output again.

Enabling Output Caching

You can enable the output caching for an action method or controller by adding an [OutputCache] attribute as shown below:

[OutputCache(Duration=20, VaryByParam="none")]
public ActionResult Index()
 ViewBag.Message = DateTime.Now.ToString();
 return View();

The output of the Index() action method will be cached for 20 seconds. If you will not defined the duration, it will cached it for by default cache duration 60 sec. You can see the cache effect by applying the break point on index method as shown below.

OutputCache Filter Parameter

Specify the name of the output cache policy which is defined with in <outputCacheSettings> tag of Web.config.
Specify the time in sec to cache the content
Specify the location of the output to be cached. By default location is Any.
Enable/Disable where to use HTTP Cache-Control. This is used only to protect very sensitive data.
Specify the database and table name pairs on which the cache content depends on. The cached data will expire automatically when the data changes in the database.
Specify the semicolon separated list of character set (Content encoding like gzip and deflate) that the output cache uses to vary the cache content
Specify the list of custom strings that the output cache uses to vary the cache content.
Specify the semicolon separated list of HTTP header names that are used to vary the cache content.
Specify the semicolon separated list of form POST or query string parameters that are used to vary the cache content. If not specified, the default value is none.

Output Caching Location

By default, content is cached in three locations: the web server, any proxy servers, and the user's browser. You can control the content's cached location by changing the location parameter of the OutputCache attribute to any of the following values: Any, Client,Downstream, Server, None, or ServerAndClient.

By default, the location parameter has the value Any which is appropriate for most the scenarios. But some times there are scenarios when you required more control over the cached data.

Suppose you want to cache the logged in use information then you should cached the data on client browser since this data is specific to a user. If you will cached this data on the server, all the user will see the same information that is wrong.

You should cache the data on the server which is common to all the users and is sensitive.

Configure Cache Location

For configuring the cache location, you need to add the System.Web.UI namespace on your controller. You can cached the user's personal information in his browser like as below.

[OutputCache(Duration = 7200, Location = OutputCacheLocation.Client, VaryByParam = "none", NoStore = true)]
public ActionResult Index()
 ViewBag.Message = "Welcome : " + User.Identity.Name;
 return View();
What do you think?

I hope you have got what is caching and how it works. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Free Interview Books
Hadoop Development cum Administration (Online)

7:00 AM-9:00AM IST

Know More
28 MAY
ASP.NET Core with Angular4 Development (Classroom)

10:30 AM-11:30 AM Weekend

Know More
27 MAY
ASP.NET MVC with Angular4 Development (Classroom)

4:00 PM-5:30 PM Weekend

Know More
25 MAY
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

Know More
25 MAY
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

20 MAY
Angular2 and Angular4 Development (Online)

08:00 AM-10:00 AM IST(+5.30 GMT) Weekend

20 MAY
NodeJS with Angular4 Development (Classroom)

5:00 PM-6:30 PM Weekend

ASP.NET MVC with Angular4 Development (Online)

07:00 AM-09:00 AM IST(+5:30 GMT) M/W/F

ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST( TUS, THR)

29 APR
ASP.NET MVC with Angular2 Development (Classroom)

9:30 AM-11:00 AM

22 APR
MEAN Stack 2 Development (Classroom)

11:00 AM-12:30 PM Weekend

10 APR
Master Class ASP.NET MVC 5 with Angular2 Plus Angular4 Development (Online)

09:00 PM - 11:00 PM IST (M/W/F)

25 MAR
Xamarin Forms : Build Cross-platform Apps (Classroom)

8:00 AM-9:30AM Weekend