Episerver Caching - The Output Cache Explained

When it comes to improving the performance of your website, performance optimisation is kind of a dark arts subject. One thing that is usually a given, is that if you try and need to do something custom then you're probably in for an interesting journey. Under the hood, Episerver uses a lof of the standard.NET caching.  In today's guide, I'm going to cover how to enable output caching in your Episerver website.  This post is the first in a series of applying output caching to your solution.

How Output Caching Works - For Dummys

Output caching is the .NET way of caching your pages and blocks HTML snippets to improve performance.  Output caching works in two flavours, full page caching and partial page output cache differently.  In Episerver, a page and a block can both use full page output cache.  Full page cache refers to anything that has a controller.  In Episerver a block doesn't necessarily need to have a controller, so some blocks can also be classed as MVC partials.  So, depending on how you define your blocks, the way .NET will cache it behind the scenes may be different. In .NET terms a full output cache is managed by the output cache module and works with a controller.  The 'partial' output cache (no controller) uses MemoryCache out of the box which was introduced in .NET 4.  In theory you can change this to use Redis or a third party cache database.

Output Cache Profile

In case you are new to output caching, the concept is pretty simple.  In your web.config, you create a cache profile. The configuration might looks like this:
      <outputCache enableOutputCache="true">
          <add name="ClientResourceCache" enabled="true" duration="3600" varyByParam="*" varyByContentEncoding="gzip;deflate" />
The enableOutputCache is useful in terms of deployment.  When you enable caching.. your pages cache.  Enabling output cache in your development environment creates a nightmare because the page caches prevents you from seeing new code changes.  Instead, you can use this property to turn on and off caching between environments. In the cache profile configuration, the varyByParam is a very useful attribute to be aware of.  The varyByParam property is a semicolon-separated list of params that is used to uniquely identify a cache object.  A cached object might be a whole page or a whole block for example.  In a 'normal' scenario queryByParams might relate to Url segments or query string parameters.  IN Episerver though, things like Visitor groups and store in memory, so we need to add in custom code to include these custom parameters.

Output Cache Attribute

To enable output caching on your page, you need to apply the 'ContentOutputCache' attribute to either the controller, or, the actions in your controller you want to cache, e.g.
    public class StartPageController : PageController<StartPage>
        public ActionResult Index(StartPage currentPage)
The next part of the process is to enable the response headers on your page.  If you don't do this you will see a no-cache set in your HTML requests response header.  To do this is pretty simple.  At some point on a page load you need to add the following code:
        public void SetResposneHeaders()

On most of my projects I usually create a base controller.  This code would be good to live in there and get triggered on each page request. If you compile your website and load it in a browser, the page should cache.  In order to check this, if you look in the pages response header: episerver_setting_cache_control You will see the Cache-control attribute is now set. This means that your page is cached :) Also, if you look in the Request header you may see the no-cache attribute set.  You do not need to worry about this, this is fine and dandy. This is part of the process of your browser talking to the server.  This does not mean that your page is not cached server side.


In today's guide, we've covered what the output cache is and how to enable it in Episerver. Caching can be a complex beast but getting the basics up and running is fairly trivial.  All you need to do is create a cache profile in your web.config, apply the output cache attribute on the page or block controller that you want to cache and then on every page request, set the cache expiration policies.  After that you're golden.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top
var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-35662136-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();