contact us.
call us.
join us.
we respect your data
At Sagittarius, we want to share our passion and excitement for digital. By providing your details you agree to be contacted by us.
We will treat your personal data with respect and you can find details in our Privacy Statement - this includes:
- What information do we collect about you
- How will we use the information about you
- Access to your information and correction
call us.
join us.
win with us.
We exist to make your business thrive and our greatest reward is our returning clients. Our focus is and always will be on our clients and not on industry awards and accreditations, which could account for why we’ve won so many of them…
Adding HtmlCache Viewing Feature to Sitecore.Rocks Visual Studio Extension.

Anton Tishchenko
After writing an article about viewing Sitecore HTML cache, I thought that it is not convenient to move custom code from one project to another. When, instead, there is ability to extend widely used existing Sitecore development tool Sitecore.Rocks then it should be done. I decided to contribute into it.
First of all, you need to fork Sitecore.Rocks project on GitHub.
Sitecore.Rocks works like server-client application. You have server side (Sitecore.Rocks.Server, connector in other words) that is copied to all Sitecore websites and client side: interface in Visual Studio to display different things from Sitecore. Fortunately, there is already “Data” column for cache viewer to display cache details and we don’t need to modify interface to add something new. But this column shows only path to item, when cache key is Sitecore identifier. In all other cases Data column will be empty. Let’s extend amount of data that could be shown in this column.
Logic that returns data to be displayed in this table located in /src/Sitecore.Rocks.Server/Requests/Caches/GetCacheKeys.cs file. We can modify it and add ability to return cache value when cache type is string or id:
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.IO;
using System.Reflection;
using System.Xml;
using Sitecore.Caching;
using Sitecore.Caching.Generics;
using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Diagnostics;
using Sitecore.Extensions.StringExtensions;
namespace Sitecore.Rocks.Server.Requests.Caches
{
public class GetCacheKeys
{
[NotNull]
public string Execute([NotNull] string cacheName)
{
Assert.ArgumentNotNull(cacheName, nameof(cacheName));
var writer = new StringWriter();
var output = new XmlTextWriter(writer)
{
Formatting = Formatting.Indented,
Indentation = 2
};
output.WriteStartElement("cacheKeys");
var cache = CacheManager.GetNamedInstance(cacheName, 0, false);
if (cache != null)
{
Write(output, cache);
}
output.WriteEndElement();
return writer.ToString();
}
private void Write([NotNull] XmlTextWriter output, [NotNull] ICache cache)
{
Debug.ArgumentNotNull(output, nameof(output));
Debug.ArgumentNotNull(cache, nameof(cache));
Database database = null;
var n = cache.Name.IndexOf("[", StringComparison.Ordinal);
if (n >= 0)
{
var databaseName = cache.Name.Left(n);
try
{
database = Factory.GetDatabase(databaseName);
}
catch
{
database = null;
}
}
//MethodInfo to gets the entry represented by key
//Parameter: The cache key
//Returns: the entry (or null)
//public ICacheEntry
var getEntryMethod = typeof(Cache
foreach (var cacheKey in cache.GetCacheKeys())
{
output.WriteStartElement("key");
output.WriteAttributeString("key", cacheKey.ToString());
output.WriteAttributeString("size", string.Empty);
output.WriteAttributeString("lastAccessed", string.Empty);
var pathValue = string.Empty;
if (database != null)
{
pathValue = GetDbPathValue(database, cacheKey);
}
if (!String.IsNullOrEmpty(pathValue))
{
output.WriteValue(pathValue);
}
else
{
try
{
var cacheEntry = getEntryMethod.Invoke(cache, new[] {cacheKey});
var cacheValue = cacheEntry.GetType().GetProperty("Data").GetValue(cacheEntry);
var cacheType = cacheEntry.GetType().GetProperty("Data").GetValue(cacheEntry).GetType();
if (cacheType == typeof(string))
{
output.WriteValue(cacheValue);
}
if (cacheType == typeof(ID))
{
output.WriteValue(cacheValue.ToString());
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error("Could not get values for cache: "+ cache.Name, ex);
}
}
output.WriteEndElement();
}
}
///
/// Returns path if key is ID and empty string if it is not
///
/// Database where item is located
/// Cache key, that could be ID
///
private string GetDbPathValue([NotNull] Database database, [NotNull] object key)
{
Debug.ArgumentNotNull(database, nameof(database));
Debug.ArgumentNotNull(key, nameof(key));
var id = key.ToString().Left(Constants.GuidLength);
if (!ID.IsID(id))
{
return string.Empty;
}
var item = database.GetItem(id);
if (item != null)
{
return item.Paths.Path;
}
return string.Empty;
}
}
}
view raw GetCacheKeys.cs hosted by GitHub
After building a solution, we need copy Sitecore.Rocks.Server.dll to bin folder of our website. Now, you are able to see what it inside cache. If you are interested in viewing other types of cache(not only string and id) then you are able to extend code above with your needs.

Anton Tishchenko
If my pull request is accepted, then this feature will be available in one of next Sitecore.Rocks version.
want to speak to one of our experts?
