Daniel Ovaska
Feb 1, 2016
  3707
(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
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

Preview multiple Visitor Groups directly while browsing your Optimizely site

Visitor groups are great - it's an easy way to add personalization towards market segments to your site. But it does come with it's own set of...

Allan Thraen | Sep 26, 2022 | Syndicated blog

The Report Center is finally back in Optimizely CMS 12

With Episerver.CMS.UI 12.12.0 the Report Center is finally re-introduced in the core product.

Tomas Hensrud Gulla | Sep 26, 2022 | Syndicated blog

Dynamic Route in ASP.NET Core When MapDynamicControllerRoute Does Not Work

Background Creating one of the add-on for Optimizely I had to deal with challenge to register dynamically route for the API controller. Dynamic rou...

valdis | Sep 25, 2022 | Syndicated blog