ASP.NET MVC 4 output cache with EPiServer 7 Preview

This blog post is about how to implement ASP.NET MVC 4 output cache with EPiServer 7 Preview.

Output cache in ASP.NET MVC 4

Output cache in ASP.NET MVC is built on top of the ASP.NET framework. Though ASP.NET MVC manages full page output cache and partial page output cache differently. Partial page output cache is equivalent to traditional web form user control output cache. See my blog post on EPiServer CMS Fragment Caching for more information on the topic.

To use page output cache in ASP.NET MVC we mark an action in a controller with the OutputCache attribute. The content rendered by the action will then be added to the output cache. The same way we can add the OutputCache attribute to child actions to support fragment caching where we don’t want to cache the entire output of the page.

But from here full page output cache and partial page output cache are managed differently by the ASP.NET MVC framework. In short the full page output cache is managed by the output cache module traditionally used by web forms, and the partial page output cache is implemented with the MemoryCache-object introduced in .NET 4.

Because of how ASP.NET MVC manages the partial page output cache we cannot add EPiServer page data cache dependencies or alter the key which is used to identify and retrieve the cached content.

By default ASP.NET MVC uses the route as key to identify output cache items, this is a problem for us since by the use of friendly URLs multiple page instances can have the same route as the route is defined by the page type.

Target criteria

  • The output cache item should depend on the page data instance it was generated for
  • The output cache should be identified by page ID and route instead of just route
  • User specific content should not be added to the output cache
  • Partial page output cache should hook into the default output cache provider also used by full page output cache

ASP.NET MVC Extensible Donut Caching

ASP.NET MVC Extensible Donut Caching by DevTrends is an output cache framework for ASP.NET MVC 3 or later. Partial page output cache and full page output cache are not managed differently, instead they hook into the same default output cache provider.

Another great function of the ASP.NET MVC Extensible Donut Caching framework is that we can use full page output cache and exclude specific child actions from being added to the output cache – also known as donut caching. We do this by using the extension method to Html.Action where the first parameter is the child action to be rendered and the second parameter declares if the action should be excluded from the output cache. See sample code below.

There are other cool benefits of using the ASP.NET MVC Extensible Donut Caching framework such as being able to use output cache profiles for partial page output cache. Go read about them in the blog post Donut Output Caching in ASP.NET MVC 3 by DevTrends.

EPiServer 7 ASP.NET MVC 4 output cache provider

The ASP.NET MVC Extensible Donut Caching framework met the following criteria:

  • User specific content should not be cached. This was available before in ASP.NET MVC, but now we can add an entire page to the output cache while also excluding child actions that contain user specific content
  • Partial page output cache also hooks into the default output cache provider

This is awesome. But we still need to depend the partial page output cache to the page data instance and identify the output cache by page ID and route instead of just route. We do this by creating our own output cache provider where we can add our output cache items to the application cache along with cache dependencies and cache key. See sample code below. I’ve left out using-statements and XML comments for brevity.

We need to set the default provider in web.config so that the ASP.NET MVC Extensible Donut Caching framework will use our custom output cache provider instead of the default ASP.NET MVC provider. Add the following outputCache-element as a child node to the caching-element.

In action

From the sample markup earlier the output is as presented below if the user isn’t logged in to the CMS. When an editor publishes a new version of the page the output cache is ejected from the application cache since it depends on the page data instance.

mvc-output-cache

Wrapping it up

The use of output cache can significantly improve a site’s performance if heavy logic is performed by one or more controllers. Where EPiServer has been selected as the CMS for heavy duty operations the proper use of output cache is a key factor in the success of the website.

Also; when implementing responsive web design performance is critical when dealing with mobile users – make that site go like greased lightning!

Thanks for reading!

daniel
daniel
Developer
Recent Posts
  • William

    Great post Daniel ! But as Far as AppFabric cache is concerned, most people still believe that it has to come up a long way as compared to it competitors, specially if we are talking about the larger web garden. Watch this video for reference which talks about the comparison between NCache and Appfabric.
    http://www.alachisoft.com/ncache/demos/ncache-vs-appfabric.html

    How do you respond to this Daniel? Love to have your thoughts over it.

    Cheers,

    • Thanks William!

      I’m afraid I haven’t looked into NCache and haven’t compared it – myself – to AppFabric Cache. The challenge lies with EPiServer’s caching strategy and how you can implement your own cache component using EPiServer’s pluggable runtime cache feature.

      I wrote a post about AppFabric & EPiServer: http://bergdaniel.se/azure-appfabric-cache-with-episerver-cms

      Since my knowledge of NCache is limited I’m not sure if it could be implemented in a different fashion.

      • William

        Daniel,
        I really appreciate your quick response. Yes I agree with you that it’s mostly depends upon your caching strategies.

  • Kayvan

    Great post. Thanks.
    Is there a way in EpiServer 7 to control the output cache duration like EpiServer6 which was in the configuration file?

  • David Sandeberg

    Great post! Thanks for putting this code up there. Works lika a charm BUT when used on a page action method that renders shared blocks it fails and renders a blank page. Nothing to do with your code but probably EPiServer not working well with the donut cache. I’m thinking the last post in this tread has something to do with it: http://mvcdonutcaching.codeplex.com/discussions/358305. Anyone got the donut cache to work with shared blocks?

Contact Us

We're not around right now. But you can send us an email and we'll get back to you, asap.

Not readable? Change text. captcha txt

Start typing and press Enter to search