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)
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?
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:
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));
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) ).
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.
Hahaha, actually not stupid at all, @Johan.
_cacheKeyCreator.CreateVersionCacheKey(content.ContentLink)
returns the content id NOT including version.
So, there we have the problem! That is a definitely a bug!
The CreateVersionCacheKey method requires a ContentReference as a parameter.... Now, I feel stupid, but I don't understand you question, @Quan?
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
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.
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?
This issue came back to life. Solution can be found here: https://getadigital.com/blog/obtaining-a-cache-dependency-key/
I am using the following code to cache the model of a block:
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:
Have anyone seen similar behavior?