2

I got a question from a client asking if we can enable/disable caching at a page level in Sitecore, I know we can do it for single renders/ but I couldn't find anything about enabling/disabling caching for a certain page.

Thanks

3
  • 1
    Are you referring to HTML caching, caching in the data provider layer, or both? Depending on what you want, the answer varies.
    – Matt
    Nov 20, 2016 at 16:41
  • It's also worth seriously considering whether this is something you should do; because I really would not advise it.
    – Matt
    Nov 20, 2016 at 16:42
  • Hey Matt, I'm asking about both, as in when we disable caching for a certain page, everything related to it should be requested from the server, the idea is to test certain areas within the website as we had a couple of issues with caching when deploying to multiple Azure publishing targets
    – Muradious
    Nov 21, 2016 at 8:09

2 Answers 2

1

You can achieve this by setting the caching programmatically. Please read my post here how to set the caching via code.

In brief, you need to hook into the rendering pipeline, know which page is being rendered, then set your caching accordingly on the main div to cache all the inner components.

Edit 1

Even if you are using webforms or renderings, the page will consist of different components. You may have a component on the page that you need to put in cache which regroups all the inner components. Then either programmatically or via the presentation details, if you cache the main components all the inner ones will be cached.

3
  • 1
    Can you elaborate on what you are saying? Your blog is about caching sublayouts which is not what the OP is asking.
    – Gatogordo
    Nov 20, 2016 at 19:01
  • But if I disable it for the main component will all children have caching disabled for them?
    – Muradious
    Nov 21, 2016 at 12:39
  • If you have set caching on both the main component and inner components, and you disable the main one, it will still cache the inner components but if the inner components are not cache and the main component cache is disable then the inner ones will not be cached Nov 21, 2016 at 12:43
1

Assuming you are using Sitecore MVC renderings, the following example will disable caching of all renderings on the page , You can use the following example, by first extending your page template and adding a checkbox field to it, Name it "Disable Caching", Then you need to override "Sitecore.Mvc.Pipelines.Response.RenderRendering.SetCacheability" processor as follows:

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 (Sitecore.Context.Item != null &&!String.IsNullOrEmpty(Sitecore.Context.Item["Disable Caching"])
                && Sitecore.Context.Item["Disable Caching"] == "1")
            {
                return false;
            }
            return base.IsCacheable(rendering, args);
        }
    }
}

Then you need to add the patch config file :

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

Hope this helps!

2
  • 3
    Did you test this? I didn't, but I can't see how this would enable caching of layouts (which is not an ootb feature). Could be my mistake, but would still mean that a little elaboration would improve the answer ;)
    – Gatogordo
    Nov 20, 2016 at 19:10
  • Thanks, I'll check if this does the trick for the page level caching
    – Muradious
    Nov 21, 2016 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.