Let Episerver editors control the output cache

The “Content output cache” is strangely enough a basically undocumented feature of Episerver CMS. It’s strange, because the content output cache can be a simple way to really speed up your site.
I will not go into details about how to set up Content output cache in this article. Instead I’ll show a way to let the Episerver editor control if output cache should be used or not.

UPDATE: See comments below.

I recently had a requirement that an Episerver editor should be able to turn off the outputcache for specific pages. By default the pages are using output cache, but if the editor adds a block/feature/personalization that doesn’t work together with output cache, then the editor can switch off output cache for that specific page.

keepcalm

To support this I created the “EditableContentOutputCacheAttribute”, which is a slightly modified version of Episervers own ContentOutputCacheAttribute:

The attribute is added to the controllers that should return cached pages:

Remember that you need to configure the cache in web.config.

<episerver>
<applicationsettings httpcacheability="Server" httpcacheexpiration="1:0:00" <="" p=""></applicationsettings></episerver>

...

 

For this attribute to work you need the IDisableOutputCache interface. You add that interface to the page types where the editors should be able to switch off the output cache.

Now the editor can turn off the output cache for a specific page!

disablecache

 

Will this affect performance?

As you might have noticed, the Episerver page data is fetched by using the url. That code-snippet will run for every page request.
I’ve done some simple load testing to see if this will affect performance compared to using the regular outputcache, It was hard to get a clear indication how much this affected performance, because the difference was so small.
I would say it has very little impact on performance.

at time of writing I was using Episerver CMS 11.11.3.0

 

 

5 thoughts on “Let Episerver editors control the output cache

  1. Hi All,
    I implemented this and it was a great improvement. However, I experienced a bug when a form is on a page. The page was set to disable cache, first load of the page is fine and caching is ignored. However if you submit the form and cause a validation error, the page gets cached with the contents of the form. It seems when a validation occurs:

    if (content is IDisableOutputCache page)
    {
    useCache = page.DisableOutputCache != true;
    }

    fails, ie the if statement returns false.

    However a breakpoint before this of content.Property[“DisableOutputCache”] does contain the property. I am not sure exactly why the IF statement fails but has something to do with the validation.

    Might be just me, but worth checking if you are using this code. Thanks for sharing code like this Erik and again I hope its just me this is happening to.

    • Thanks for raising that issue Paul. I haven’t tested that scenario. Do I understand you right that it works if you check content.Property[“DisableOutputCache”] instead of page.DisableOutputCache?

  2. Hi Erik, yes in the debugger after submitting a validation error… before the IF call to (content is IDisableOutputCache page) the content.Property[“DisableCache”] returned correctly.

Leave a Reply to Erik HCancel reply