November Happy Hour will be moved to Thursday December 5th.

Mattias Lövström
Jul 30, 2010
  16266
(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

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

Comments

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

Ted
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

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

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 SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog