How to use MOSS output caching with custom parameters


In MOSS there are three different types of caching available:



  • Output caching

  • BLOB Cache

  • Object Cache

You can find a very detailed description about these three options here: http://blogs.msdn.com/ecm/archive/2006/11/08/how-to-make-your-moss-2007-web-site-faster-with-caching.aspx .


If you want to use output caching for the anonymous user of your site but you want to cache these pages also based on some custom parameters like cookie values or query string parameters you should follow these steps:



  1. Go to the "Site Collection Cache Profiles" on the site settings of your web application and edit the properties of the profile "Public Internet (Purely Anonymous)". In the "VARY BY CUSTOM PARAMETER" section you can enter your custom parameters e.g. "CookieValues" and "Country" (semicolon separated):


    2.    The next step is to tell your web application about your CustomCachingHttpApplication in the global.asax file:


<%@ Assembly Name="Microsoft.SharePoint"%>
<%@ Assembly Name="<assemblyfullname>" %>
<%@ Import Namespace="<your_namespace>" %>
<%@ Application Language="C#" Inherits="<your_namespace>.CustomCachingHttpApplication" %>


    3.   The last step is implementing this CustomCachingHttpApplication class:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.ApplicationRuntime;
using Microsoft.SharePoint;


namespace <your_namespace>
{
   
public class CookieCachingHttpApplication : SPHttpApplication,
                                                IVaryByCustomHandler 
    {
     
public override void Init()
     
{
       
base.Init();
        this.RegisterGetVaryByCustomStringHandler
             ((Microsoft.SharePoint.ApplicationRuntime.IVaryByCustomHandler)this);
     
}


public string GetVaryByCustomString(HttpApplication app, HttpContext context, string custom)
{
string[] strings = custom.Split(';');
string cacheKey = string.Empty;


      foreach (string arg in strings)
     
{
        
if (arg.ToLower() == "cookievalues")
        
{
           
HttpCookie countryCookie = context.Request.Cookies
              [System.Web.Configuration.WebConfigurationManager.AppSettings["CookieCountry"]];


            HttpCookie languageCookie = context.Request.Cookies
              [System.Web.Configuration.WebConfigurationManager.AppSettings["CookieLanguage"]];


            if (countryCookie != null && languageCookie != null)
           
{
               
cacheKey += countryCookie.Value + languageCookie.Value;
           
}
        
}


         if (arg.ToLower() == "country")
        
{
            
cacheKey += context.Request["Country"];
        
}


         return cacheKey;


     }
   }
}


With that your pages are now cached depending on the cookie values "CookieCountry" and "CookieLanguage" and the query string parameter "Country".

Skip to main content