Virtual Happy Hour canceled for Friday May 31st.

Try our conversational search powered by Generative AI!

Praful Jangid
Nov 13, 2019
  2976
(9 votes)

Disabling Cache for Test Driven (page with active A/B test) content

Caching is a great feature to improve the site performance. But recently we faced issue with test driven content being cached. The user was seeing the same test content that get loaded first because it get cached. And, there were no good solution to vary content based on test driven content. So, we came up with a solution to disable cache for test driven content (page with active A/B testing).

In order to do that you need to find if there is any active test for context item. I created an attribute by inheriting from default ContentOutputCacheAttribute.

public class CustomContentOutputCacheAttribute : ContentOutputCacheAttribute
{
     private static readonly Injected<IMarketingTestingWebRepository> _marketingTestingWebRepository;

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

    private static bool UseOutputCache(IPrincipal principal, HttpContextBase context, TimeSpan duration)
    {
        var url = context.Request.Url?.ToString();
        var content = UrlResolver.Current.Route(new UrlBuilder(url));
        if (content == null) return false;
        return !_marketingTestingWebRepository.Service.GetActiveTestsByOriginalItemId(content.ContentGuid).Any();
    }
}

Now, next step is apply this attribute to your Page controller Index method. You can see the test content is not loading from cached.

public class StoryPageController : PageController<StoryPage>
{
    [CustomContentOutputCache(Duration = 7200)]
    public ActionResult Index(StoryPage currentPage)
    {
        return this.View(currentPage);
    }
}

Now, open your browser and check in network tab if the current page Response Headers > Cache-Control value. If you have any active test running for current page then the value should be private otherwise public with max-age=7200 [value set by you for how long you want to cache].

Thanks and regards

Happy Coding

Nov 13, 2019

Comments

Please login to comment.
Latest blogs
Blazor in Optimizely CMS 12 with .NET 8

How to enable support for Blazor components in Optimizely CMS 12 after upgrading to .NET 8.

Ted | May 30, 2024 | Syndicated blog

Build a headless blog with Astro and Optimizely SaaS CMS

I’m a big fan of using the right tool for the right job. I’m also a big fan of Astro , for the right use case. Let's explore Astro to see what it's...

Jacob Pretorius | May 28, 2024

Microsoft announces Natural language to SQL

Finally, Microsoft launches "Natural language to SQL," after it has been available for several months in Optimizely CMS!

Tomas Hensrud Gulla | May 23, 2024 | Syndicated blog

Five easy ways to start personalizing your content right now

If you clicked on this article, you already know that getting the right message to the right person at the right time helps drive conversions and...

Kara Andersen | May 23, 2024