Try our conversational search powered by Generative AI!

Issue with cache dependency

Vote:
 

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? 

#189490
Mar 20, 2018 11:12
Vote:
 

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)

#189492
Mar 20, 2018 11:36
Vote:
 

The documentation is quite confusing: https://world.episerver.com/documentation/Items/Developers-Guide/Episerver-CMS/9/Caching/Object-caching/

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

#189493
Mar 20, 2018 11:48
Vote:
 

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. 
#189494
Mar 20, 2018 11:53
Vote:
 

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[]
{
    this.contentCacheKeyCreator.VersionKey,
    CacheKeys.NewsCommon, // Just a constant all news share
    CacheKeys.IndexingCommon // Just a constant everything that is index by Find shares
};

this.synchronizedCache.Insert(
    cacheKey,
    newsArticles,
    new CacheEvictionPolicy(
        expiration: TimeSpan.FromMinutes(30),
        timeoutType: CacheTimeoutType.Absolute,
        cacheKeys: null,
        masterKeys: masterKeys));
#189504
Mar 20, 2018 13:33
Vote:
 

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) ). 

#189508
Mar 20, 2018 14:01
Vote:
 

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.

#189512
Mar 20, 2018 14:06
Vote:
 

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

_cacheKeyCreator.CreateVersionCacheKey(content.ContentLink)

returns the content id NOT including version.

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

#189525
Mar 20, 2018 14:39
Vote:
 

Are you sure content.ContentLink contains versionid? 

#189526
Mar 20, 2018 14:42
Vote:
 

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

#189527
Mar 20, 2018 14:51
Vote:
 

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 

#189528
Edited, Mar 20, 2018 14:53
Vote:
 

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.

#189548
Mar 20, 2018 15:09
Vote:
 

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

#189550
Mar 20, 2018 15:21
Vote:
 

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?

#189553
Mar 20, 2018 15:34
Vote:
 

I'll look into it.

#189560
Edited, Mar 20, 2018 16:20
Vote:
 

This issue came back to life. Solution can be found here: https://getadigital.com/blog/obtaining-a-cache-dependency-key/

#208153
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.