Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

Issue with cache dependency


I am using the following code to cache the model of a block:

 _objectInstanceCache.Insert(cacheKey, model, new CacheEvictionPolicy(new TimeSpan(1, 0, 0), CacheTimeoutType.Absolute));

This works as expected. Now if I add a cache dependency (to content version) the model is never fetched from the cache, _objectInstanceCache.Get(cacheKey) always return null! I use the following code to add to the cache with cachedependency:

var cacheDepKeys = new List() { _cacheKeyCreator.CreateVersionCacheKey(content.ContentLink) };
_objectInstanceCache.Insert(cacheKey, model, new CacheEvictionPolicy(new TimeSpan(1, 0, 0), CacheTimeoutType.Absolute, cacheDepKeys));

// _cacheKeyCreator is of type Episerver.Core IContentCacheKeyCreator 

Have anyone seen similar behavior? 

Mar 20, 2018 11:12

My memory is a bit rusty now, but I think if the cacheKeys refer to objects which are not in the cache, then the cache itself is not inserted. For your purpose, you should go with masterKeys instead 

public CacheEvictionPolicy(TimeSpan expiration, CacheTimeoutType timeoutType, IEnumerable<string> cacheKeys, IEnumerable<string> masterKeys)

Mar 20, 2018 11:36

The documentation is quite confusing:

So what you are saying is that the cacheDependency (version) should be the masterKey?

Mar 20, 2018 11:48

IIRC the default implementation will add an empty object for each master key, but it will not add the cache value if the cache key points to a non existing cache object.

So you can either:

  • Use the master keys. 
  • Add the object to the dependent cache key, first. 
Mar 20, 2018 11:53

Following code works for me. In this case I'm just invalidating the cache as soon as any content is changed, but changing it to a content version key should work too.

var cacheKey = "yadayada";

var masterKeys = new[]
    CacheKeys.NewsCommon, // Just a constant all news share
    CacheKeys.IndexingCommon // Just a constant everything that is index by Find shares

    new CacheEvictionPolicy(
        expiration: TimeSpan.FromMinutes(30),
        timeoutType: CacheTimeoutType.Absolute,
        cacheKeys: null,
        masterKeys: masterKeys));
Mar 20, 2018 13:33

I tried that, @Johan with a content version key. The problem then is that the cache is not invalidated, I get the cached version even if I have created a new version of the block.

I found a workaround, which is to include the the content version as part of the cache key (_cacheKeyCreator.CreateVersionCacheKey(content.ContentLink) ). 

Mar 20, 2018 14:01

Maybe a stupid question... but does your ContentLink contain version information? I guess it does...

I've never been that granual. I usually go with the CreateVersionCommonCacheKey instead and that has been working for me.

Mar 20, 2018 14:06

Hahaha, actually not stupid at all, @Johan. laughing


returns the content id NOT including version.

So, there we have the problem! That is a definitely a bug!

Mar 20, 2018 14:39

Are you sure content.ContentLink contains versionid? 

Mar 20, 2018 14:42

The CreateVersionCacheKey method requires a ContentReference as a parameter.... Now, I feel stupid, but I don't understand you question, @Quan? 

Mar 20, 2018 14:51

I meant the ContentLink can be either "123" (without version), or "123_456" (with version)

I checked the default implementation of IContentCacheKeyCreator and I would be very very surpised if there is a bug in CreateVersionCacheKey which skip version part in the cachekey 

Edited, Mar 20, 2018 14:53

I would also be suprised if there's a bug in that function. I'm more curious if content.ContentLink contains the neccessary version information the function needs to create the key.

Mar 20, 2018 15:09

(currentBlock as IContent).ContentLink does not include the version id. 

Mar 20, 2018 15:21

That's your issue then, and not a bug :)

You need to load the block in a specific version I guess, but isn't there a way to get the version from an already loaded content item?

Mar 20, 2018 15:34

I'll look into it.

Edited, Mar 20, 2018 16:20

This issue came back to life. Solution can be found here:

Oct 15, 2019 14:28
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.