SaaS CMS has officially launched! Learn more now.

Praful Jangid
Nov 13, 2019
  3006
(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
Optimizely SaaS CMS Concepts and Terminologies

Whether you're a new user of Optimizely CMS or a veteran who have been through the evolution of it, the SaaS CMS is bringing some new concepts and...

Patrick Lam | Jul 15, 2024

How to have a link plugin with extra link id attribute in TinyMce

Introduce Optimizely CMS Editing is using TinyMce for editing rich-text content. We need to use this control a lot in CMS site for kind of WYSWYG...

Binh Nguyen Thi | Jul 13, 2024

Create your first demo site with Optimizely SaaS/Visual Builder

Hello everyone, We are very excited about the launch of our SaaS CMS and the new Visual Builder that comes with it. Since it is the first time you'...

Patrick Lam | Jul 11, 2024

Integrate a CMP workflow step with CMS

As you might know Optimizely has an integration where you can create and edit pages in the CMS directly from the CMP. One of the benefits of this i...

Marcus Hoffmann | Jul 10, 2024

GetNextSegment with empty Remaining causing fuzzes

Optimizely CMS offers you to create partial routers. This concept allows you display content differently depending on the routed content in the URL...

David Drouin-Prince | Jul 8, 2024 | Syndicated blog

Product Listing Page - using Graph

Optimizely Graph makes it possible to query your data in an advanced way, by using GraphQL. Querying data, using facets and search phrases, is very...

Jonas Bergqvist | Jul 5, 2024