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
Customizing Property Lists in Optimizely CMS

Generic property lists is a cool editorial feature that has gained a lot of popularity - in spite of still being unsupported (officially). But if y...

Allan Thraen | Oct 2, 2022 | Syndicated blog

Content Delivery API – The Case of the Duplicate API Refresh Token

Creating a custom refresh provider to resolve the issues with duplicate tokens in the DXC The post Content Delivery API – The Case of the Duplicate...

David Lewis | Sep 29, 2022 | Syndicated blog

New Optimizely certifications - register for beta testing before November 1st

In January 2023, Optimizely is making updates to the current versions of our certification exams to make sure that each exam covers the necessary...

Jamilia Buzurukova | Sep 28, 2022

Optimizely community meetup - Sept 29 (virtual + Melbourne)

Super excited to be presenting this Thursday the 29th of September at the Optimizely community meetup. For the full details and RSVP's see the...

Ynze | Sep 27, 2022 | Syndicated blog