Try our conversational search powered by Generative AI!

Mattias Lövström
Jul 30, 2010
  16029
(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
Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog

The A/A Test: What You Need to Know

Sure, we all know what an A/B test can do. But what is an A/A test? How is it different? With an A/B test, we know that we can take a webpage (our...

Lindsey Rogers | Apr 15, 2024

.Net Core Timezone ID's Windows vs Linux

Hey all, First post here and I would like to talk about Timezone ID's and How Windows and Linux systems use different IDs. We currently run a .NET...

sheider | Apr 15, 2024

What's new in Language Manager 5.3.0

In Language Manager (LM) version 5.2.0, we added an option in appsettings.json called TranslateOrCopyContentAreaChildrenBlockForTypes . It does...

Quoc Anh Nguyen | Apr 15, 2024