Add Sitecore Caching Programmatically

Caching is a very important feature for a site to have a good performance. Sitecore has several caches that can be set when adding sublayouts or renderings via the presentation details. A developer will normally set the caching on the standard items which will be automatically be set when content authors creates the items.

However, if the content authors configures a sublayout that require caching, the following issues may arise:

  1. The content author forgets to set the caching.
  2. The content author does not know which caching to set.
  3. The proper caching is not set.

Due to this, your components may not function as required. Lets us take an example, you have a header sublayout and in the header, it shows the username of a person. If this header is cached, the problem that will occur is that other users may see the same username. This is not appropriate because the username is considered as a PII Data.

During this post, I will explain how you can easily set the caching programmatically when the sublayout is being rendered. The advantage when doing it through code is that it will prevent caching that were not set properly to be changed when the sublayout / rendering is being rendered on the page.

So lets us code now. The first thing to do is to identify which pipeline is responsible for the rendering of the sublayouts / renderings. The pipeline is the InsertRenderings. So, we will create a class which will inherits the InsertRenderingsProcessor as follows:

public class RenderingProcessor : InsertRenderingsProcessor
{
}

Now we need to override the Process method.

public class RenderingProcessor : InsertRenderingsProcessor
{
    public override void Process(InsertRenderingsArgs args)
    {
    }
}

Below is a sample code which disable the caching on the Sitecore default Sample Sublayout.

public override void Process(InsertRenderingsArgs args)
{
    if (args.ContextItem == null)
    {
        return;
    }

    DeviceItem device = Context.Device;

    if (device == null)
    {
        return;
    }

    var renderings = args.Renderings.ToList();

    args.Renderings.Clear();

    foreach (var renderingReference in renderings)
    {
        if (renderingReference.RenderingID == new ID("{885B8314-7D8C-4CBB-8000-01421EA8F406}"))
        {
            renderingReference.RenderingItem.Caching.Cacheable = false;
            renderingReference.Settings.Caching.Cacheable = false;
        }
    }

    args.Renderings.AddRange(renderings);    
}

From this code you can easily define your caching strategy on different sublayout the before the sublayout is rendered, you make it pass through your code. The last step is to setup the configuration file for your method to trigger.

 <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
     <insertRenderings>
        <processor patch:after="processor[@type='Sitecore.Pipelines.InsertRenderings.Processors.AddRenderings, Sitecore.Kernel']"
          type="YourNamespace, YourAssemblyName" />
      </insertRenderings>
    </pipelines>
  </sitecore>
</configuration>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s