How to Check What is Inside Sitecore HTML Cache? .

how-to-check-what-is-inside-of-sitecore-html-cache-header

Sitecore has a lot of caches: AccessResultCache, DataCache, DeviceItemsCache, HTML cache, ItemCache, ItemPathsCache, PathCache, RegistryCache, RuleCache, StandardValuesCache, ViewStateCache, XslCache, FieldReaderCache and others. When you facing with caching issue on your website(something is either not cached, or cached but should not), it is important to have the ability to see what is present in cache.

All Sitecore caches consist of cache key and cache value. Cache key is always string. Cache value could be any type of object depending on cache. We can take as example HtmlCache cache. When you see what cache key you have and what data is there, you can better tune caching of controls, especially when you have custom(not out of the box) cache criterias (vary by IP, vary by country, vary by role, etc.) and a lot of controls on a page. Here is code snippet to get everything inside HtmlCache: 

 

using System;

using System.Collections;

using System.Collections.Generic;

using System.Reflection;

using System.Web;

using Sitecore.Caching;

namespace Sitecore.Utils

{

public partial class HtmlCacheList : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

var websiteCache = GetCaches("website");

WebsiteCache.DataSource = websiteCache;

WebsiteCache.DataBind();

}

private List<tuple<string,>> GetCaches(string sitename)</tuple<string,>

{

var list = new List<tuple<string,>>();</tuple<string,>

HtmlCache htmlCache = CacheManager.GetHtmlCache(Sitecore.Sites.SiteContextFactory.

GetSiteContext(sitename));

if (htmlCache != null)

{

FieldInfo fi = typeof (CustomCache).GetField("_cache", BindingFlags.NonPublic | BindingFlags.Instance);

var cache = (Cache) fi.GetValue(htmlCache);

var fi2 = typeof (Cache).GetField("data", BindingFlags.NonPublic | BindingFlags.Instance);

var hashTable = (Hashtable) fi2.GetValue(cache);

foreach (var cacheKey in hashTable.Keys)

{

list.Add(new Tuple<string,>(cacheKey.ToString(), HttpUtility.HtmlEncode(((Cache.CacheEntry) hashTable</string,>

[cacheKey]).Data.ToString())));

}

}

return list;

}

}

}

 


It requires using reflection due to private methods in
HTML cache. The result of execution GetCaches method will be List<Tuple<string, string>>, that you can filter, make search or display on the page. It can save you a lot of time when debugging Sitecore caching issues. Here is an example of service page that displays cache keys and cache values:

how-to-check-what-is-inside-of-sitecore-html-cache-2


If you can’t add this code to some secure page in your solution(you don’t want to do it or need some out of the box solution) then there are also other useful tools for troubleshooting issues with Sitecore cache. But they will not show you the cached value of HtmlCache entry:

want to speak to one of our experts?

Anton Tishchenko Thumbnail
Anton Tishchenko
Head of Digital Engineering
Anton has worked as a developer since 2007, he is a highly experienced Sitecore developer who previously worked as a Technical Team Lead at Sitecore. Anton's expertise in the Sitecore platform is formidable; he's definitely one of the world's finest Sitecore ninjas and in 2019 he was recognised as the only Sitecore MVP in the Ukraine when he achieved his Technology MVP Status.
Anton Tishchenko Thumbnail

Anton Tishchenko

03 May 2019 - 6 minute read
share this

stay in the know, stay ahead.

Get the latest from the agency, including news, events and expert content.
explore services in the article
find out what we can do for you
read some of our case studies