Add custom cache headers

This article demonstrates how to add custom cache headers in addition to the Sitefinity CMS default ones. For more information, see Advanced settings of cache profiles.

You do this by adding the setNoTransform cache header in a specific cache profile:

  1. Navigate to Administration » Settings » Advanced » System » Output cache settings » Page Cache profiles.
  2. Choose profile and open the Parameters page.
  3. Click Add new.
  4. In the Key field, enter setNoTransform.
  5. Set the value to true.

Next, you add a CustomPageOutputCacheStrategy in your project. Use the following code:

C#
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using Telerik.Sitefinity.Web;

namespace SitefinityWebApp
{
    public class CustomPageOutputCacheStrategy : PageOutputCacheStrategy
    {
        public override bool ApplyServerCache(HttpContextBase context, Telerik.Sitefinity.Modules.Pages.Configuration.IOutputCacheProfile profile, PageSiteNode page)
        {
            var baseCacheSettings = base.ApplyServerCache(context, profile, page);
            var cache = context.Response.Cache;

            //check for setNoTransform parameter
            if (PageOutputCacheStrategyHelper.ContainsKey(profile.Parameters, "setNoTransform"))
            {
                if (profile.Parameters.Get("setNoTransform") == "true")
                {
                    cache.SetNoTransforms();
                }
            }

            return baseCacheSettings;
        }
    }
}

In the code above, you use the PageOutputCacheStrategyHelper. You implement it in the following way:

C#
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;

namespace SitefinityWebApp
{
    public static class PageOutputCacheStrategyHelper
    {
        public static bool ContainsKey(this NameValueCollection @this, string key)
        {
            return @this.Get(key) != null
                || @this.Keys.Cast<string>().Contains(key, StringComparer.OrdinalIgnoreCase);
        }
    }
}

Finally, register the CustomPageOutputCacheStrategy in the Global.asax file:

C#
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Data;
using Telerik.Microsoft.Practices.Unity;
using Telerik.Sitefinity.Services;

protected void Application_Start(object sender, EventArgs e)
{
    SystemManager.ApplicationStart += SystemManager_ApplicationStart;
}

private void SystemManager_ApplicationStart(object sender, EventArgs e)
{
    ObjectFactory.Container.RegisterType<PageOutputCacheStrategy, CustomPageOutputCacheStrategy>(new ContainerControlledLifetimeManager());
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
New to Sitefinity?