MasterCacheKeys vs IContentCacheKeyCreator

Vote:
 

I'm in the process of caching some commerce content with dependencies, and I'm not sure if I should use MasterCacheKeys or IContentCacheKeyCreator to create cache dependencies. Looking at the source codes, it appears that the generated strings are completely different, so I guess they fulfill different purposes.

What's the difference between them and when should each be used?

For simplicity, I'm using CatalogContentLoader.GetCacheMasterKeys() to create cache keys, which in turn makes use of MasterCacheKeys.

#210961
Edited, Dec 13, 2019 13:57
Vote:
 

Hi Andreas

I wrote this a little while back about cache keys and master keys: https://stefanolsen.com/posts/caching-custom-data-that-depends-on-episerver-content/

Generally, individual cache keys are good if you want to depend on a specific piece of content. Master keys are good if you want to depend on any or all instances of a kind. That is for CMS.

When caching data based on a commerce entry, I would typically depend on master keys. Unless I find that I can depend on a more specific cache key.

#210966
Dec 14, 2019 8:28
Andreas J - Dec 15, 2019 22:08
This is not a question about whether to use cache keys or master keys. MasterCacheKeys is a class that can be used to create both cache keys and master keys, and since that functionality overlaps with IContentCacheKeyCreator, it's confusing which class to use.

"When caching data based on a commerce entry, I would typically depend on master keys" - On what master keys?
Stefan Holm Olsen - Dec 16, 2019 5:07
Code sample taken from my blog post:
int objectId = _referenceConverter.GetObjectId(contentReference);
string masterKey = MasterCacheKeys.GetEntryKey(objectId);

I use that over the IContentCacheKeyCreator thing, because it is in the core of Commerce.
Andreas J - Dec 16, 2019 9:23
MasterCacheKeys.GetEntryKey(objectId) must for sure be considered not a master key, but yes, this is indirectly my current solution.
Vote:
 

for Commerce you should be using MasterCacheKeys. For catalog content, there might be dependencies on IContentCacheKeyCreator but that is transparent to you. 

#210967
Dec 14, 2019 14:22
Andreas J - Dec 15, 2019 22:13
How do you mean it's transparent to me?
* 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.