Mattias Lövström
Jul 30, 2010
(1 votes)

Caching object EPiServer is easier then you probably think

It very often I see EPiServer sites using the CacheManager.RuntimeCache or ASP.NET runtime cache to store “hard to get objects” in - this will not update cached objects on remote site on a web farm.

To get the remote events to update caches on all machine a recommend to use EPiServer.CacheManagers static methods

// To add a cached object on the local machine


// To get a local stored cache object


// To remove a cache object on all machines on the web farm


And NEVER use the methods starting with RuntimeCache… (if you are not absolutely sure that you want to use the local cache)

Jul 30, 2010


Magnus Rahl
Magnus Rahl Sep 21, 2010 10:33 AM

Ah, I thought it was the other way around! *Learned something today* Thanks!

Sep 21, 2010 10:33 AM

Caching in EPiServer IS easier than I thought. Thanks Mattias :)
/ Tobias Nilsson

egilagre Sep 21, 2010 10:33 AM

Can you please elaborate on why it is better to use Episerver.Cachemanager rather than HttpRuntime.Cache?

I would think that using Microsoft's own caching technology would give an upper hand in the future when microsofts updates their .Net compiler and ASP .NET :)

Sep 21, 2010 10:33 AM

Episerver.CacheManager ensures that cache updates are load balanced (in load balanced and Enterprise environments). i.e. that cache is updated on all machines serving requests for that site. The EPiServer CMS cache mechanism is built on top of the normal ASP.NET cache so any changes by Microsoft in the future will still effect EPiServer.

/ Paul Smith

Ted Sep 21, 2010 10:33 AM

I was under the impression that CacheManager.RuntimeCacheAdd() was a wrapper for HttpRuntime.Cache.Add() with the addition of triggering remote events?

If not then I definitely learned something really important about the CacheManager class today! :)

Thanks, Mattias!

Tore Gjerdrum
Tore Gjerdrum Sep 21, 2010 10:33 AM


A am a fan of this post,, and I some times cache collections with a dependency to the EPiServer DataFactoryCache.VersionKey. But when using EPiServer.CacheManager.Add(), this is not possible. Is there a reason why there is no way of sending a CacheDependency to the Add method?

I see that I can use EPiServer.CacheManager.RuntimeCacheAdd(string key, object value, CacheDependency dependencies), but this post states that: "And NEVER use the methods starting with RuntimeCache… (if you are not absolutely sure that you want to use the local cache)".

So is there a way around this?

Br, Tore

Sep 21, 2010 10:33 AM

Great question, Steves blog using the version key of the cache and it’s great but in the most scenarios it going to invalidate too much on the server (sense it’s updates for each PageData changes in the cache).
But the Add method in the cache manager only adds an object to the cache. The magic happens in the Cachemanager.Remove method – it propagates out that an object are removed from the cache to all machines in the web farm.
So another way of solving this is to call the CacheManager.RemoveRemoteOnly(string key) when a cache object is going to be removed by creating a cache item remove callback method, and add it directly with the runtime cache.
THIS IS DANGEROUS because this method will also be executed when the memory gets full. If this function is slow (the RemoveRemoteOnly function access the network) the cached objects can’t be removed as fast as the system needs and we get an Out Of Memory exception.

Sep 30, 2010 05:23 PM

Am I missing something here? This is the implementation of CacheManager.Add(string, object):

public static void Add(string key, object item)
RuntimeCache.Add(key, item, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal);

So, for adding to the cache, it really does not matter which one you use. You can safely use one of the overloaded RuntimeCache methods to control the lifetime.

Caching is local. Cache invalidation is distributed, so the only method you need to stay away from is the RuntimeCacheRemove, if you're not sure what you're doing.

I humbly disagree on your statement in the previous comment - that it will invalidate too much. For most sites, PageData objects are not changed that often, and it will buy you better performance in most cases. I argue that your statement is valid for the minority of cases, not most cases, but for the few cases - when it does matter, it is very important that you know what your doing. Btw. when was the last time Average Joe experienced an Out of Memory exception on his site?

Greg B
Greg B Oct 20, 2010 12:12 PM

I've got to agree with Steve Celius here. isn't a big part of caching managing the cached objects lifetime? and as such, doesn't the CacheManager.Add method fail in two senses.

1. It assumes you want to cache everything indefinitely.
2. If you don't, you have to manage the expiration manually, rather than being able to use a CacheDependency or SqlCacheDependency...

Please login to comment.
Latest blogs
Optimizely For you Intranet

Having been at Optimizely and in the CMS industry for nearly 16 years I have seen clients’ intranet requirements go from a simple site just to hous...

Robert Folan | Sep 22, 2023

Vulnerability in EPiServer.GoogleAnalytics v3 and v4

Introduction A potential security vulnerability was detected for Optimizely Google Analytics addon (including EPiServer.GoogleAnalytics and...

Bien Nguyen | Sep 20, 2023

Overriding Optimizely’s Content Recommendations Block to Implement Custom Recommendations

Introduction The Content Recommendations add-on for Optimizely CMS dynamically recommends content from your site tailored to the interests of each...

abritt | Sep 13, 2023 | Syndicated blog

Developer contest! Install the AI Assistant and Win Bose QC45 Headphones!

We are thrilled to announce a developer contest where you have the chance to win a pair of Bose Headphones. The goal is to be the first developer t...

Luc Gosso (MVP) | Sep 7, 2023 | Syndicated blog

Send Optimizely notifications with SendGrid API, not SMTP

If your Optimizely site already sends transaction emails through an email platform API, why not do the same with Optimizely notification emails?

Stefan Holm Olsen | Sep 6, 2023 | Syndicated blog

Optimizely Configured Commerce Custom POST API

Introduction When creating custom API controllers for an Optimizely B2B project it’s possible you’ll want to create POST calls. Following the...

Dylan Barter | Sep 6, 2023