3

I've read the documentation regarding caching and also a number of posts on SSE and I'm trying to understand if there is anything else to consider other than the following when enabling caching for a page by turning off the DisableBrowserCaching option:

  • Personalisation - of any components on any of the pages we wish to Cache
  • Content changes - If we have dynamic content (which we do)

As I understand it by default Sitecore pages are not cached due to the dynamic nature of Sitecore pages it is unlikely that the data source, device, authentication status, user, rendering parameters and/or query string parameters are the same as a previous request.

Therefore instead caching should really be set on specific components instead.

We are looking to cache a number of key/high-traffic pages only (e.g the homepage) for 1 or 2 minutes as most of the components on this page are dynamic so are not cached by Sitecore - but we are happy that the content might be a minute or two out of date for the performance gains we might get for a full page cache.

Other than the content not being immediately up-to-date (if updated in Sitecore or from a 3rd party) is there anything else to think about or side-affects to be aware of?

Further info: We are not using Personalisation at all currently. Sitecore 8.1 update 2.

0

2 Answers 2

2

When I was in a similar situation, what I did was to create a custom cache key and extended it off of the VaryByData cache option for renderings.

What you can do then is to based on the dynamic properties that makes your dynamic contents (UserGroupId, Affiliate, etc...) you can generate a cache just for that situation. In the example below, based on the info in the visitors cookie, we cache the renderings differently. This caches the output (HTML) of the rendering.

The code

using Sitecore;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Mvc.Pipelines.Response.RenderRendering;
using Sitecore.Mvc.Presentation;

namespace Customer.Mvc.Theme.Pipelines.mvc.renderRendering
{
    public class GenerateCacheKey : Sitecore.Mvc.Pipelines.Response.RenderRendering.GenerateCacheKey
    {
        protected override string GenerateKey(Rendering rendering, RenderRenderingArgs args)
        {
            var cacheKey = base.GenerateKey(rendering, args);

            if (rendering.RenderingItem.ID == new ID(Constants.Renderings.Product.ProductGrid))
            {
                var caching = rendering.Caching;

                var cookie =
                    System.Web.HttpContext.Current.Request.Cookies[
                        Foundation.Constants.Strings.AffiliationCookieName];

                if (caching.VaryByData && !string.IsNullOrWhiteSpace(cookie?.Value))
                {
                    cacheKey += "_#affiliateId:" + cookie.Value;
                }
            }

            return cacheKey;
        }
    }
}

The Config

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <mvc.renderRendering>
        <processor patch:instead="processor[@type='Sitecore.Mvc.Pipelines.Response.RenderRendering.GenerateCacheKey, Sitecore.Mvc']"
                     type="Customer.Mvc.Theme.Pipelines.mvc.renderRendering.GenerateCacheKey, Customer.Mvc.Theme"/>
      </mvc.renderRendering>
    </pipelines>
  </sitecore>
</configuration>
2

I would never use DisableBrowserCaching as it will cache media files on browser, nothing to do with html pages.

For Sitecore driven content, we should leverage HTML Caching on renderings with relevant Vary By options. While using it, take care for below things:

  1. Define HTML cache Size as per its utilization. Use the /sitecore/admin/cache.aspx or /sitecore/admin/stats.aspx to know the utilization.
  2. Choose the Vary By option wisely. Refer - [http://www.xcentium.com/blog/2016/09/23/sitecore-caching-and-how-it-works-in-mvc-solutions][1]
  3. Never cache renderings which needs postbacks, if you use so, post backs won't work.
  4. Make sure HTML Cache is getting cleared on every publish. It gets cleared on publish:end:remote event on CD servers.

While using Dynamic Content, never apply Sitecore caching on renderings. Try to use .NET Time based cache to serve dynamic content. So that the cache will be cleared after provided internal of time automatically. See below example -

Cache.Insert(cacheKey, response, null, System.DateTime.Now.AddMinutes(10), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.NotRemovable, null);
5
  • Yogesh - are you sure that this setting (DisableBrowserCaching) is nothing to do with the pages and is only to do with media files? as the docs say otherwise: sdn.sitecore.net/upload/sitecore6/sc62keywords/… - 'The DisableBrowserCaching setting in the web.config file controls whether Sitecore instructs clients to cache entire pages.' Commented Aug 22, 2017 at 12:59
  • Thanks for sharing this Adam. I Agree that Sitecore has mentioned its usage here, but it's not working as expected at least me (Sitecore 8.2 update-2). Even I tried this on older versions i.e. 6.2, etc. as well) I mean disabling this setting sets the Cache-Control to Private, but if you see the cache expiry header, it''s an old date (year of 1969), means browser won't cache this. Commented Aug 22, 2017 at 13:34
  • Hmm this is very odd. I've tried setting <setting name="DisableBrowserCaching" value="false" /> and <setting name="DisableBrowserCaching" value="true" /> and it makes no difference. I get "Cache-Control: private" regardless. When it is true Sitecore is meant to return "Cache-Control: no-cache, no-store Pragma: no-cache" it doesn't seem to for me. I've even tested this on a new OOTB install of Sitecore 8.2 update 5. Maybe I need to open a support ticket about this? Commented Aug 22, 2017 at 16:48
  • Support came back to me to say they can't replicate this issue and to check: 1. the iis headers (we have none setup) 2. web.config customHeaders (we have none setup) 3. layout/rendering caches (these are disabled for my page as far as I can tell). I have tested locally with the out of the box /sitecore/templates/Sample/Sample Item template though and interestingly I do get "Cache-Control: no-cache, no-store" for pages using that template. However this uses an ASP.Net webforms layout (/layouts/Sample Layout.aspx) and not MVC so perhaps there is something in that. I will carry on looking... Commented Aug 24, 2017 at 16:49
  • Yogesh - Support have since confirmed that caching not being set correctly is a bug with 8.1 Update 2 MVC (reference number #116813). I have implemented a fix for this with some guidance from support and it now sets the cache to "Cache-Control: no-cache, no-store Pragma: no-cache" when DisableBrowserCaching is true. I'll write up a blog post on this to share it when I get a minute. Commented Sep 18, 2017 at 11:07

Your Answer

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

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