Context Dependent Output Caching


Output caching is a great feature for improving the overall performance of your website. However, there are cases where the page is simply too dynamic to utilize this useful feature. Kentico again has a solution for you. You can create context dependent output caching with a few lines of code.

At first, I’d strongly suggest you check out my caching webinar (it’s a bit outdated, however, most of the things discussed still apply). You’ll find out what types of caches are available and how to utilize them best in your project. Now back to this specific use case. Imagine having a pretty busy website, which additionally requires you to use content personalization. Luckily, all content personalization is based solely on Kentico Personas (elements on your pages are only dependent on the current persona and no other variables are used). That’s pretty important, otherwise, our implementation will not work properly, as other variables would have to be included into the cache key as well. I have already let the secret slip, we need to alter the cache key for our output cache based on the current persona. By the way, other variables can be used as well, such as cookie values or contact groups. Going back to our example, let’s say that personas are the only variable used, so our code will be fairly simple. We need to implement a custom cache helper, overriding three basic methods for cache manipulation:




We will simply append another cache key variable at the end of the cache key, if the cache is the output cache. To check that, we’ll just check for the “outputdata” keyword in the cache key. The last step is, as mentioned, getting and appending the persona ID, for example, to that key. Your code could look something like this (FYI: most of the code was taken from our examples under c:\Program Files (x86)\Kentico\9.0\CodeSamples\App_Code Samples\Samples\Classes\CustomCacheHelper.cs):

using System; using System.Web.Caching; using CMS; using CMS.EventLog; using CMS.Helpers; using CMS.OnlineMarketing; // Custom helper registration. Uncomment the following line to enable the custom helper. [assembly: RegisterCustomHelper(typeof(CustomCacheHelper))] /// <summary> /// Saves different cache per persona. /// </summary> public class CustomCacheHelper : CacheHelper { protected override void InsertInternal(string key, object value, CMSCacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback, bool useFullKey) { base.InsertInternal(AlterKey(key), value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback, useFullKey); } protected override object GetInternal(string key, bool useFullKey) { return base.GetInternal(AlterKey(key), useFullKey); } protected override object RemoveInternal(string key, bool useFullKey) { return base.RemoveInternal(AlterKey(key), useFullKey); } private string AlterKey(string key) { // alter the key only for the output cache if (key.StartsWith("outputdata")) { if (OnlineMarketingContext.CurrentContact != null && OnlineMarketingContext.CurrentContact.ContactPersonaID != null) { return key + "|persona=" + OnlineMarketingContext.CurrentContact.ContactPersonaID; } } return key; } }

This code will now generate a unique cache key based on the current persona (You can check your cache in Debug > Cache items):

1.pngAs mentioned above, you can use other caching variables too, depending on your requirements. Please note that this is only a proof of concept and the example may be improved. You may, as always, get in touch with our consulting department if you have any specific questions regarding your project at



Share this article on   LinkedIn

Boris Pocatko

Senior Solution Architect for Kentico