Level Up: Sitecore HTML Cache

Last week I blogged about the power of Sitecore’s HTML Cache. I have found it so powerful that I become frustrated when I come across a component that cannot be cached. In this post, I’d like to introduce a couple of minor enhancements to extend its functionality.

1. Cache Only GET Requests

I’ve run into a number of scenarios where I could not cache a component purely because it contained a form or handled a postback operation (Think of a header component containing a textbox for site search). Sometimes this can be avoided by breaking the component into multiple components, where you minimize the amount of the page that can be cached. Often, however, refactoring the component can be difficult or impossible. I’ve added a checkbox to the ‘Caching’ section of the ‘Rendering’ template. When this checkbox is selected, all cacheablity options will be respected for GET requests but ignored for all other request types.

cacheget

2. Vary By Custom

Another caching dilemma arises when the existing ‘Var By’ options do not meet the needs of my component. Thus, I’m left with a component I can’t add to the HTML cache. To solve this, I’ve added a new ‘Vary By’ option, called ‘Vary By Custom’. Selecting this option, coupled with your own custom cache definition, can cache whatever you’d like.

varybycustom

The value of the ‘Vary By Custom Type’ field is required when selecting ‘Vary By Custom’ and should follow the format: ‘Namespace.ClassName, Assembly’. The class definition provided will need to implement the ‘ICustomCacheable’ interface. The ‘ICustomCacheable’ interface looks like this:

1
2
3
4
5
6
7
namespace Sitecore.HtmlCache.CustomCache
{
  public interface ICustomCacheable
  {
    string GetCustomCacheKeyPart();
  }
}

Just implement ‘GetCustomCacheKeyPart()’ as needed and you are ready to go. When the module tries to create an instance of your custom class, it first tries to use ASP.NET MVC’s DependencyResolver. So, if you are making use of an IoC container, you can make use of dependency injection in your custom class.

Get The Module

If you want the Sitecore package or check out the code, head over to my GitHub page: https://github.com/OptimizedQuery/Sitecore.HtmlCache. To start using, just grab the latest package in the /sitecore folder and install. That’s it!

**Important Notes**

  • The master branch in GitHub is compiled against Sitecore v7.5 rev 150212. There is also a branch for Sitecore v7.2 141226.
  • These features are currently only available for Sitecore MVC renderings.
  • These features are currently only available when set directly on the rendering. It does not yet support custom cache settings at the item level.
  • This is all Proof of Concept code and has not been used or tested in a production environment. Use your best judgment before adding to your project.

One thought on “Level Up: Sitecore HTML Cache

  1. I never noticed the Cache GET Only setting. That really could come in handy for a lot of form pages. Also never really looked at vary by custom. That’s a great features.

    Nice work.

Leave a Reply