Disable caching for a rendering on some pages

Everyone loves Sitecore caching capabilities, especially HTML Caching, where you can cache the output HTML of any  rendering.

When you want to cache any rendering, you have two options:
  1. On rendering definition item, Set 'Cacheable' checkbox 
  2. On item presentation details, Select your rendering and set 'Cacheable' checkbox.
The problem with the first approach is that once you set the 'Cacheable' option on definition item, any page that uses this rendering will always cache it, and you can't cancel the caching for that rendering on some pages that you dont want it to be cached there.

I found a solution for this, where you can disable any caching setting for any rendering, :
  1.  Create a new rendering parameters template which includes a checkbox field called "Cancel Cache Settings'
  2. Go to your rendering definition item, and set the parameter template to the new template
  3. If your site runs on Sitecore MVC, Do the following
    • Create a class called 'SetCacheability' :
      namespace Sitecore.SharedResources.Pipelines.Rendering
      {
          public class SetCacheability : Sitecore.Mvc.Pipelines.Response.RenderRendering.SetCacheability
          {
              protected override bool IsCacheable(Sitecore.Mvc.Presentation.Rendering rendering, Sitecore.Mvc.Pipelines.Response.RenderRendering.RenderRenderingArgs args)
              {
                  if (!String.IsNullOrEmpty(rendering.Parameters["Cancel Cache Settings"])
                      && rendering.Parameters["Cancel Cache Settings"] == "1")
                  {
                      return false;
                  }
                  return base.IsCacheable(rendering, args);
              }
          }
      }
    • Create a config file inside your 'Include' folder:
      <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
        <sitecore>
          <pipelines>
            <mvc.renderRendering>
              <processor patch:instead="processor[@type='Sitecore.Mvc.Pipelines.Response.RenderRendering.SetCacheability, Sitecore.Mvc']"
                type="Sitecore.SharedResources.Pipelines.Rendering.SetCacheability, [YOUR ASSEMBLY NAME]"/>
            </mvc.renderRendering>
          </pipelines>
        </sitecore>
      </configuration>
  4. If your code runs on ASP.NET forms:
    •  Add the following code to your sublayout code:
      private NameValueCollection _Parameters;
            public NameValueCollection Parameters
            {
                get
                {
                    if (_Parameters != null)
                    {
                        Sublayout sublayout = Parent as Sublayout;
                        _Parameters = Sitecore.Web.WebUtil.ParseUrlParameters(sublayout.Parameters);
                    }
                    return Parameters;
                }
            }
      
            public override bool Cacheable
            {
                get
                {
                    if (!string.IsNullOrEmpty(Parameters["Cancel Cache Settings"]) && Parameters["Cancel Cache Settings"] == "1")
                    {
                        return false;
                    }
                    return base.Cacheable;
                }
                set
                {
                    base.Cacheable = value;
                }
            }
Now you can disable HTML caching for any rendering on the pages you dont want to cache.
 Leave your feedback in the comments!

Popular posts from this blog

Integrating Lucene.Net.Contrib.Spatial with Sitecore Search API (Geo-Spatial Search)

Caching Rendering HTML when conditional rules are applied

Solr with Sitecore Checklist