Brian McKeiver's Blog

Introducing the Kentico Cache Doctor NuGet Package

Update: Version 2.0 of the package was updated as of 9/1/2019, and includes Kentico.Glimpse Support.



Over the past many years, there is one pretty important thing I have learned while working with Kentico (and most other Web based CMS platforms out there). That one thing I have learned is that when it comes to problems with a page not showing the right results, or the code not working, or the content not quite lining up the right way; it almost always boils down to one of three things. Those three things in Kentico EMS are macros for dynamic content (and all the fun macro security issues that come with them), client side browser cache (where the latest Javascript file is not being loaded or is stale), and server side caching (where the CMS has placed data in the server memory and the latest page is not being sent down to the user's browser).

With Kentico's focus on MVC first for the latest Kentico 12 SP1, that last one, server side caching, is actually more important and fun to deal with than ever. The thing is with MVC, the caching strategy you use is up to you, and there are many different ways to do it. One of the most powerful ways is add what's called Output Caching into the solution. Output Caching is excellently detailed out over on Michal Samuhel's blog post on fixing a slow Kentico MVC site. If you have not read that post, STOP, and go read it right now. Then come back over here.  

In working with Kentico MVC sites for over 3 years, I have seen many issues during project development cycles that were resolved by simply clearing the page's Output Cache and/or restarting the app pool for the MVC live site. This is fine and all, but it does take a bit of time to do this and slows down the development process. To combat this I have decided to work on a little add-on for Kentico MVC development that gives the developer a quick short cut to clear the Cache without having to restart the site. Enter the Kentico Cache Doctor.


Kentico Cache Doctor Features

The Nuget package, targeted at Kentico EMS 12.0 and 12.0 SP1, installs a custom DLL in your MVC live site that adds a custom HttpModule to your application. The HttpModule injects a HttpHandler that attaches to each request and reads the current cache items out of the current HttpRuntime.Cache object. This is where anything you place in [OutputCache] attributes on your controllers or custom C# code ends up (as long as you have Location=Server set). Cache Doctor takes all of those items and dumps them to a visual display on the bottom of the page. Almost a lot like Kentico.Glimpse dumps SQL calls on the page.

Not only does it read and display the items in Output Cache for you, it also provides a Bust link for each cache item. Clicking the Bust link opens up a new tab that sends a specially routed request to clear or "touch" that cache item on the server's memory. This allows you to clear a specific item and then quickly refresh the page in the browser to ensure you are getting the latest and greatest of the page. It all ends up looking like this:


Kentico Cache Doctor for Kentico 12


Click on the Bust link above and bam, zap, bonk goes the cache item. Technically we do a call to the HttpRuntime.Cache as well as CacheHelper.TouchKey for good measure to clear that specific item out of memory on the server (taking out the variable of Kentico Caching getting in the way). Then you can simply refresh the page to see what's new.

Lastly, the display also tries to calculate the number of Output Cache items you have, as well as the size of the cache items in memory. Now, warning, everything I researched and read tells me there is no way to 100% accurately calculate the size of a live C# object in memory inside of the application code. That is what ASP.NET profilers are for. However, I still wanted to get some sense for how much RAM these objects are taking. I ended up using a not perfectly accurate method of using GC.GetTotalMemory() method that C# does have before and after creating a collection that mimics the Output Cache collection.


A Note about Output Cache Items

I have noticed that Kentico MVC sites use some pretty crazy ways to create Cache Key names and Cache Dependencies when it comes to Kentico Caching. Sometimes it makes sense, other times not so much (unless you go hunting through some of the deep Kentico source code out there). To that end I have tried to sort the Cache Keys in alpha order to make it easier to find an item, and on top of that, I have bolded the items that should directly match the page url that you are looking at for the main Output Cache item or main nodes query that the Kentico Dancing Goat starter site uses. I did this to try and make it easier to figure out which item to bust to ensure you can get a fresh page load when you want. Again, this method is not perfect, and your mileage may vary, especially if you have created some custom cache key names.


Pro Tip: I have found that the item with a key name that starts with outputcacheurltopagemapper is the main item to bust if you have leveraged the [OutputCache] attribute on your controller, while an item with a key name that starts with nodes| is typically the item to bust if you have leveraged the mOutputCacheDependencies.AddDependencyOnPages<PageType>() that comes with the Dancing Goat starter site code base.



Try out Kentico Cache Doctor Now

The Mcbeev.Kentico.CacheDoctor NuGet package is available now at It works for all .net framework versions 4.6.1 to 4.7.2. I thought that made sense because that is basically what Kentico 12.0 supports for MVC these days. Although soon we should be looking forward to .Net Standard and .Net Core for Kentico. Maybe even in time for Kentico Connections 2019

I have open sourced all of the code for the custom module on the project's Github repo. I threw this togeheter fairly quickly, so some refactoring could surely help.


Kentico Cache Doctor for Kentico 12 MVC


To be clear though, this is a developer tool only. Luckily it is very easy to uninstall. You simply have to unistall the package from you NuGet package list, and remove the one line of web.config in the applicationSettings node.



I've tested out the Kentico MVC NuGet package on about 5 different sites and variants of K12 and different .Net frameworks, but by no means would I say it is 100% fool proof. If you have issues, or do find things that don't make sense, feel free to comment on the blog post here. You can also submit an issue on my Github repo for the code. Hopefully you can avoid any fun Kentico Caching issues with this tool. Thanks and happy Kentico MVC debugging.