Click here to Skip to main content
15,886,026 members
Articles / Web Development / HTML

Cheat Sheet for Enabling Output Caching in ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
4 Jul 2014CPOL2 min read 9.3K   6  
Cheat sheet for enabling output caching in ASP.NET MVC

What is Output Caching

ASP.NET output caching feature allows increasing web application performance by returning content that was built some time ago instead of rebuilding it on every request. Returning content from cache only takes a few milliseconds as opposed to executing a full request cycle that could take much longer.

The content is being cached on the ASP.NET level and the request would not reach the application code for the content that is still in cache. ASP.NET output caching can be used in both WebForms and MVC applications and using it in MVC has become even easier than before.

When to Use Output Caching

Output caching is useful when content returned by a controller action method does not change frequently, requires more than few CPU cycles or database access and does not require a lot of memory to store. For example, I would not recommend to use output caching for a large binary object like an image or a file. Also, there is no point to cache a short string that only takes a few milliseconds to build. The best use case would be an average size content that requires some calculation or database access to produce but does not change on every request.

How to Enable Output Caching

The easiest way to enable output caching in MVC is using an OutputCache attribute on the controller or controller action method. Applying output caching on a controller action method is recommended as it gives much better granularity and control over output caching. The best way to control output caching behaviour is via caching profiles that allow defining all parameters in a web.config file and override them for each environment where the web application is deployed (i.e., Dev, QA, Stage, Live, etc.)

Limitations

ASP.NET MVC has some limitations for output caching, for example: caching profile is not supported for a partial/child controller method therefore caching parameters including Duration and VaryByParam must be set in the code.

Implementation and Code Review Check Points

  1. Apply to individual action methods
  2. Use Caching profiles
  3. Partial/child action methods should not be cached for too long
  4. Disable output caching in Dev/QA/Stage environment.
  5. Do not forget to set Duration and VaryByParam values in web.config

Example

HTML
<script class="brush: xml" type="syntaxhighlighter"><system.web> <caching> <outputcachesettings> <outputcacheprofiles> <add duration="60" name="cacheProfile" varybyparam="*"> </add></outputcacheprofiles> </outputcachesettings> </caching></system.web></script>
<script class="brush: csharp" type="syntaxhighlighter">
[OutputCache(CacheProfile = "cacheProfile")]
public ActionResult Test()
{ ViewData["result"] = "This text was rendered at " + DateTime.Now; return View(); }
</script>

This article was originally posted at http://feeds.feedburner.com/Webnet

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Canada Canada
Alexander Turlov is a professional software development consultant that has been working in IT industry since 1987. His programming experience includes such languages as FORTRAN, Pascal, Basic, C, C++ and C#. He's been working for different industries including but not limited to science, manufacturing, retail, utilities, finance, insurance, health care, education and so on. His area of professional interests is cloud powered rich web applications development with .NET, C#, ASP.NET/MVC and JavaScript. He is working in software development doing architecture, design and development on .NET platform and using Microsoft Visual Studio, Azure and Visual Studio Team Services as his primary tools. He holds a M.Sc. degree in physics and various industry certifications including MCSD.NET, Azure and Scrum.

View my profile on LinkedIn

View my blog

Comments and Discussions

 
-- There are no messages in this forum --