Categories
Bloggers
Blogs RSS feed

Extending Page Route handler – configuring the page output cache to be non-browser specific

by Kaloyan Savov

Configuring output cache is very important part of the performance tuning process of your website. Sitefinity offers an easy way to configure output cache and there is a documentation article available providing detailed information about this.

When you request a Sitefinity page our PageRouteHandler class checks whether there is such a cached version of the page and serves it. If the page is not present in cache we will add it and serve it from there on the next request. By default when you try to access a page of your website using different browsers, separate output cache will be created for every browser. To recognize which browser currently requesting particular page PageRouteHandler relies on browser’s user-agent variable.

In this blog post I will demonstrate how you can change the default behavior and implement a custom page route handler that will create non-browser specific cache. To achieve this we need to create a new route handler that will inherit from PageRouteHandler. The method responsible for managing output cache is ApplyServerCache and we will need to override it in order to change its default behavior. This behavior can be modified by changing the value of UserAgent property. To achieve this we will need to use reflection as .NET does not allow for setting false value once we've set it to true.

As I already mentioned above we need to create a new class that will inherit from PageRouteHandler and then override ApplyServerCache method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Web;
using Telerik.Microsoft.Practices.Unity;
using System.Collections.Specialized;
 
namespace SitefinityWebApp
{
    public class CustomPageRouteHandler : PageRouteHandler
    {
        protected override bool ApplyServerCache(HttpContextBase context, Telerik.Sitefinity.Modules.Pages.Configuration.OutputCacheProfileElement profile, PageSiteNode siteNode)
        {
            var result = base.ApplyServerCache(context, profile, siteNode);
            //Need to use reflection as .NET does not allow for setting false value once we've set it to true
            var headers = context.Response.Cache.VaryByHeaders;
            var headersCollection = (NameObjectCollectionBase)headers.GetType().GetField("_headers", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(headers);
            var method = headersCollection.GetType().GetMethod("BaseRemove", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            method.Invoke(headersCollection, new string[] { "User-Agent" });
            return result;
        }
 
        public static void RegisterType()
        {
            ObjectFactory.Container.RegisterType(typeof(PageRouteHandler), typeof(CustomPageRouteHandler));
        }
 
    }
}

Now when our new custom page route handler is implemented we just need to register it in the Global.asax file of our project.

using System;
using System.Linq;
using Telerik.Sitefinity.Abstractions;
 
namespace SitefinityWebApp
{
    public class Global : System.Web.HttpApplication
    {
 
        protected void Application_Start(object sender, EventArgs e)
        {
            Bootstrapper.Initialized += Bootstrapper_Initialized;
             
        }
 
        void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
        {
            if (e.CommandName == "RegisterRoutes")
                CustomPageRouteHandler.RegisterType();
        }
    }
}

I have prepared and a short video illustrating how our custom page route handler is working.

You can download the complete sample from here.

Leave a comment