Try our conversational search powered by Generative AI!

Daniel Ovaska
Feb 1, 2016
  4425
(6 votes)

Episerver performance–Part 1 Object Cache

When to use object caching

Object caching is great when you are getting data from external systems, like fetching pressreleases from another site like myNewsDesk, getting products from a PIM or similar. These calls are generally very slow and requesting fresh data every time might not be needed.

Memory Cache, Static Dictionary or Episerver Cache

The .NET memory cache in System.Runtime.Caching is a great tool but if you decide to use it, make sure you are not ever going to use load balancing for your websites. To invalidate cache on one server is pretty difficult to get working correctly. For multiple servers, you really want support out of the box like you get with EPiServer Cache.  

A static dictionary can work in a similar way in solutions to store data across calls. The problems here is that you need to be sure that you won’t get into memory problems for storing large amounts of data. Caches have a lot of functionality around this problem. For small amounts of data, storing them in a static dictionary can work but with the same problem with load balancing as above for the standard .NET cache.

Episerver caching now resides under the interesting name ISynchronizedObjectInstanceCache. The old CacheManager is now dead. The new implementation is easy to get from the ServiceLocator and to use and you can follow the guide here:

http://world.episerver.com/documentation/Items/Developers-Guide/Episerver-CMS/9/Caching/Object-caching/

I want to highlight a few nice additions they have done. Master keys are new and work like a grouping of cached items. This means that it’s easy to divide your cache into multiple sections like “users”, “products” and similar and only kill the part of the cache you need when something is updated. This is really useful in many scenarios.

To get full benefit from Episervers object caching you also want to configure cache invalidation events for load balanced servers. You can read more about that here

http://world.episerver.com/documentation/Items/Developers-Guide/Episerver-CMS/9/Event-management/Event-management/

To cache or not to cache… – Hamlet

Don’t use object caching if you are simply getting current page, getchildren from the CMS. This is cached already 99% of the time so the cost for these operations are normally very small if you aren’t adding something expensive on top. Measure performance before you cache anything. Caching adds an extra layer of complexity to any solution. Caching menues is normally a bad idea since the menu depends on what user is logged in. Normally you don’t need to cache this and if you do, only cache it for anonymous users.

Best practice tips about caching

Log everything

I would really recommend adding extra logging to all cache logic that specifies whether something is gotten from the cache or not.

Make it possible to get fresh from data source

Include a parameter to your cached method that forces a fresh copy from the data source..

Make it possible to change/disable cache

Preferably as an admin or at least in config files

Think about the first hit

If you want to avoid the first user having a huge load time while the cache is still empty, why not load the data with a scheduled job? You did add that extra parameter for getting fresh from data source right? Then setting up a scheduled job that triggers update will be a piece of cake.

Don't underestimate the complexity of caching

Caching stuff is easy. But it's like a sawed-off shotgun. It's easy to kill someone else by mistake than the one you are aiming at. Give some extra love to choosing the correct cachekeys that includes all parameters and set up the correct cache invalidation. It’s a steady source of errors in solutions that are quite tricky to find later without decent logging.

I’ll dive in to some heavy code later in this blog series but the links above are enough for getting caching to work.The coding part for basic object caching is pretty trivial. I’ll add some examples for the more advanced variants in the next blogs

Feb 01, 2016

Comments

Please login to comment.
Latest blogs
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

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