Wednesday, May 30, 2018

Adding HtmlCache Viewing Feature to Sitecore.Rocks Visual Studio Extension

It is copy of my article placed in Sagittarius blog

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:


    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.




If my pull request is accepted, then this feature will be available in one of next Sitecore.Rocks version.

Friday, May 4, 2018

How to Check What is Inside Sitecore HTML Cache?

It is a copy of this blog post.

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:
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:



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: