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
Sitecore Stack Exchange is a question and answer site for developers and end users of the Sitecore CMS and multichannel marketing software. It only takes a minute to sign up.
Sign up to join this communityI 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
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.
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!