It’s not a secret that cache is one of the most, if not the most, important factors to their website performance. Yes cache is great, and if you are using Optimizely Content/Commerce Cloud, you should be using ISynchronizedObjectInstanceCache
to cache your objects whenever possible.
But caching is not easy. Or rather, the cache invalidation is not easy.
To ensure that you have effective caching strategy, it’s important that you have cache dependencies, i.e. In principles, there are two types of dependencies:
- Master keys. This is to control a entire cache “segment”. For example, you could have one master key for the prices. If you needs to invalidate the entire price cache, just remove the master key and you’re done.
- Dependency keys. This is to tell cache system that your cache item depends on this or that object. If this or that object is invalidated, your cache item will be invalidated automatically. This is particularly useful if you do not control this or that object.
ISynchronizedObjectInstanceCache
allows you to control the cache dependencies by CacheEvictionPolicy
. There are a few ways to construct an instance of CacheEvictionPolicy
, from if the cache expiration will be absolute (i.e. it will be invalidated after a fixed amount of time), or sliding (i.e. if it is accessed, its expiration will be renewed), to if your cache will be dependent on one or more master keys, and/or one or more dependency keys, like this
/// <summary>
/// Initializes a new instance of the <see cref="CacheEvictionPolicy"/> class.
/// </summary>
/// <param name="cacheKeys">The dependencies to other cached items, idetified by their keys.</param>
public CacheEvictionPolicy(IEnumerable<string> cacheKeys)
/// <summary>
/// Initializes a new instance of the <see cref="CacheEvictionPolicy"/> class.
/// </summary>
/// <param name="cacheKeys">The dependencies to other cached items, idetified by their keys.</param>
/// <param name="masterKeys">The master keys that we depend upon.</param>
public CacheEvictionPolicy(IEnumerable<string> cacheKeys, IEnumerable<string> masterKeys)
The constructors that takes master keys and dependency keys look pretty the same, but there is an important difference/caveat here: if there is no dependency key already existing in cache, the cache item you are inserting will be invalidated (i.e. removed from cache) immediately. (For master keys, the framework will automatically add an empty object (if none existed) for you.)
That will be some unpleasant surprise – everything seems to be working fine, no error whatsoever. But, if you look closely, your code seems to be hitting database more than it should. But other than that, your website performance is silently suffering (sometimes, not “silently”)
This is an easy mistake to make – I did once myself (albeit long ago) in an important catalog content cache path. And I saw some very experienced developers made the same mistake as well (At this point you might wonder if the API itself is to blame)
Take away:
- Make sure you are using the right constructor when you construct an instance of
CacheEvictionPolicy
. Are you sure that the cache keys you are going to depend on, actually exist?
In newer version of CMS, there would be a warning in log if the cache is invalidated immediately, however, it could be missed, unless you are actively looking for it.
Note that this behavior is the same with ISynchronizedObjectInstanceCache
as it extends IObjectInstanceCache
.