Try our conversational search powered by Generative AI!

ContentOutputCache busting

Vote:
 

Hi,

Shouldn't the ContentOutputCache clear the output cache of the page each time the page is published? I am working on CMS verison 11.20.5 and upon publishing the page, I dont see the outputcache being purged for that page.

Below is a small tweak I made to disable/enable the output cache via CMS.

public class CustomContentOutputCacheAttribute : ContentOutputCacheAttribute
{
        public CustomContentOutputCacheAttribute()
        {
            this.UseOutputCacheValidator = UseOutputCache;
        }

        private static bool UseOutputCache(IPrincipal principal, HttpContextBase context, TimeSpan duration)
        {
            var useCache = false;
            if (!principal.Identity.IsAuthenticated && duration != TimeSpan.Zero)
            {
                if (string.Equals(context.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
                {
                    useCache = true;
                    var url = context.Request.Url?.ToString();
                    var content = UrlResolver.Current.Route(new UrlBuilder(url));
                    if (content is HomesPageBase page)
                    {
                        useCache = page.EnableOutputCache;
                    }
                }
            }

            return useCache;
        }
}

I then decorate the controller with this attribute, the caching is working fine. But the cache doesnt seem to be getting cleared when I publish the page. 

    [CustomContentOutputCache]
    public class StartPageController : PageControllerBase<StartPage>
    {
        [PageViewTracking]
        public ActionResult Index(StartPage currentPage)
        {          
            // load related content
            var model = PageViewModel.Create(currentPage);

            // Check if it is the StartPage or just a page of the StartPage type.
            if (SiteDefinition.Current.StartPage.CompareToIgnoreWorkID(currentPage.ContentLink))
            {
                //Connect the view models logotype property to the start page's to make it editable
                var editHints = ViewData.GetEditHints<LayoutViewModel, StartPage>();

                // header elements
                editHints.AddConnection(m => m.MainHeader.HeaderLogo, p => p.HeaderLogo);
                editHints.AddFullRefreshFor(x => x.HeaderLogo);

                // content elements
                editHints.AddFullRefreshFor(x => x.CommunityCards);

                // footer elements
                editHints.AddConnection(m => m.Footer.FooterColumn1Title, p => p.FooterColumn1Title);
                editHints.AddConnection(m => m.Footer.FooterColumn2Title, p => p.FooterColumn2Title);
                editHints.AddConnection(m => m.Footer.FooterColumn3Title, p => p.FooterColumn3Title);
                editHints.AddFullRefreshFor(x => x.FooterColumn2Links);
                editHints.AddFullRefreshFor(x => x.FooterColumn3Links);
                editHints.AddConnection(m => m.Footer.SocialLinkTitle, p => p.SocialLinksTitle);
                editHints.AddConnection(m => m.Footer.SpecialOffersForm, p => p.SpecialOffersForm);
                editHints.AddFullRefreshFor(x => x.FooterBottomLinks);
                editHints.AddConnection(m => m.Footer.FooterCopyright, p => p.FooterCopyright);

                HttpContext.Response.Cache.SetExpires(DateTime.Now.Add(Settings.Instance.HttpCacheExpiration));
                HttpContext.Response.Cache.SetCacheability(Settings.Instance.HttpCacheability);
                HttpContext.Response.Cache.SetValidUntilExpires(true);
            }

            return View(model);
        }
    }

Regards,

Sid

#255636
Edited, May 27, 2021 13:53
Vote:
 

Can you confirm you are using the [ContentOutputCache] attribute on your controller? 

#255637
May 27, 2021 15:07
Siddharth Gupta - May 27, 2021 15:32
Hi David,

Thanks for looking into this, I am tweaking the ContentOutputCache just a bit. I have updated my post above with the code.
Vote:
 

Have you tried removing all your custom code to check things still work as expected in your custom attribute? Also have you tried using the default [ContentOutputCache] attribute? Finally I assume you are testing this on your local machine so do not have any load balancing confuguration to think about.

#255649
May 27, 2021 19:50
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.