Blog
ASP.NET MVC 3 - OutputCache Action Filter
By Martin Schaeferle | August 14, 2012
ASP.NET has had robust output caching features since the very first version back near the beginning of the millennium. It has a rich set of features that let you control the cache by duration, characteristics of the request such as by various parameters, and even based on data changes in an underlying data store. You can cache full pages, parts of pages, or individual objects such as Datasets. And each new version of ASP.NET brings improvements and new features.
In MVC 3 you can apply the OutputCache attribute to an action method that you call using either the Html.Action or Html.RenderAction helper methods. The effect is to cache the output of the child action method independently of the page. This is an example of one of the built-in action and result filters. OutputCache has properties that hook into virtually all of the ASP.NET caching features, with the exception of the VaryByControl property, which wouldn't make sense in MVC since a server control is a bit of an irrelevant concept.
Using the OutputCache is simple. The Dog controller in the sample application has a CacheThis action method that simply returns the CacheThis view. It sets the output cache to expire every 10 seconds, and specifies that the cache will not depend on any property values. You must include the VaryByParam property in this usage.
[OutputCache(Duration=10, VaryByParam="none")] public ActionResult CacheThis() { return View(); }
The CacheThis page simply displays the current time, as shown in the following code. When you run the application and click the View Cached Page link on the Index view of the Dog controller, you'll see the page shown in Figure 16. Keep refreshing the page. For 10 seconds the time won't change, but after 10 seconds the time will update.
<h2>CacheThis</h2> Page created at @DateTime.Now.ToLongTimeString()
Figure 16. The cached page. One nice option of the OutputCache filter is that instead of defining the characteristics of the cache for every action method where you want to use caching, you can define one or more profiles in web.config. This way, when you have to change the caching characteristics, you can change them in one place rather than throughout the application. The sample application has this caching element defined in web.config to define a single CacheProfile (you can define as many as you want):
<caching> <outputCacheSettings> <outputCacheProfiles> <add name="CacheProfile" duration="10" varyByParam="none" /> </outputCacheProfiles> </outputCacheSettings> </caching>
Then you can configure the OutputCache filter to use that profile:
[OutputCache(CacheProfile="CacheProfile")] public ActionResult CacheThis() { ...
When you run the application again, you'll get the same behavior as before, with the view cached for 10 seconds. And now the application is much more maintainable.
TIP: You can't use the regular ASP.NET @OutputCache attribute in the Page directive in a view. The reason is that the action method executes before a view is even selected. The @OutputCache will work to cache the contents of the view, but the action method will still run every time. Since the method is where all of the action happens with extracting and manipulating data, the benefit of caching will be negligible if you just cache the view.
Get more training on ASP.NET MVC 2 & 3!