Views: 13703
Number of votes: 1
Average rating:

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

EPiServer.CacheManagers.Add(…)

// To get a local stored cache object

EPiServer.CacheManagers.Get(…)

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

EPiServer.CacheManagers.Remove(…)

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
(By Magnus Rahl, 9/21/2010 12:33:28 PM)

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

Guest
(By Guest, 9/21/2010 12:33:28 PM)

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

egilagre
(By egilagre, 9/21/2010 12:33:28 PM)

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 :)

Guest
(By Guest, 9/21/2010 12:33:28 PM)

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.
/ Paul Smith

Ted
(By Ted, 9/21/2010 12:33:28 PM)

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
(By Tore Gjerdrum, 9/21/2010 12:33:28 PM)

Hi,

A am a fan of this post, http://world.episerver.com/Templates/Forum/Pages/thread.aspx?id=12442, 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

mattias.lovstrom
(By mattias.lovstrom, 9/21/2010 12:33:28 PM)

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.

steve
(By steve, 9/30/2010 7:23:28 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 Brant
(By Greg Brant, 10/20/2010 2:12:59 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.