+1-888-365-2779
Try Now
More in this section
Categories
Bloggers
Blogs RSS feed

Implement caching by domain name

by Radoslav Georgiev

Sitefinity's built in caching mechanism caches cached objects by relative path, rather than by the full path. Sometimes you may need to display the same page in different sub-domains, and have different information displayed based on what sub-domain the user is on. The purpose of this blog post is to sample how to modify the built in caching providers to store keys for cached objects with a bit more information than the relative path. The code for this sample is provided by Sitefinity Senior Developer Vladimir Vasilev.

 

Sitefiniy comes with two caching providers built-in. They are declared in your web.config in the <caching> config element of the <framework> configuration section:

<caching defaultProvider="memoryCache">
    <providers>
        <add name="memoryCache" type="Telerik.Caching.MemoryCachingProvider, Telerik.Framework"/>
        <add name="ASPNET" type="Telerik.Caching.AspNetCachingProvider, Telerik.Framework" duration="120" slidingExpiration="true"/>
    </providers>
    <cacheDependency mode="InMemory"/>
</caching>

 

Then in the <cms> configuration element you set the default page provider to use either of those two providers. The default setting is to use the ASPNET caching provider, which is a wrapper of the built in ASP.NET caching provider:

<cms defaultProvider="Sitefinity" pageExtension=".aspx" disabled="false"
   pageEditorUIMode="Overlay">
   <providers>
    <clear />
    <add connectionStringName="DefaultConnection" allowPageHistory="true"
     allowPageWorkflow="false" cachingProviderName="ASPNET" name="Sitefinity"
     type="Telerik.Cms.Data.DefaultProvider, Telerik.Cms.Data" />
   </providers>

 

Based on what you have configured for the caching provider you have two apply one of the bellow described modifications. If you are using ASPNET caching provider, you just have to modify the Global.asax file to add the host name in the key generated for cached objects:

public override string GetVaryByCustomString(HttpContext context, string custom)
{
    if (custom.Equals("cms"))
    {
        return String.Concat(context.Request.RawUrl, context.Request.Browser.Type, context.Request.Url.Host).ToLower();
    }
    return base.GetVaryByCustomString(context, custom); 
}

 

If you are using the MemoryCachingProvider you have to create a custom provider which will inherit from the built-in one and extend the methods for inserting cached objects and retrieving them. Sample code can be found bellow:

using System.Web;
using Telerik.Caching;
  
/// <summary>
/// Summary description for CustomMemoryCachingProvider
/// </summary>
public class CustomMemoryCachingProvider: MemoryCachingProvider
{
    //generate new key based on relative url, domain name, and browser type
    private string GetNewKey(string oldKey)
    
        HttpContext context = HttpContext.Current;
        return string.Concat(oldKey, context.Request.Url.Host, context.Request.Browser.Type);
    }
  
    //insert the custom key instead of original one
    public override void Insert(string key, object value, System.Web.Caching.CacheDependency dependencies, int duration, bool slidingExpiration)
    {
        base.Insert(GetNewKey(key), value, dependencies, duration, slidingExpiration);
    }
  
    //get cached object based on custom key
    public override object Get(string key)
    {
        return base.Get(GetNewKey(key));
    }
}

 

After you have implemented the custom MemoryCachingProvider you have to substitute the built-in one with yours in web.config:

<framework>
    <caching defaultProvider="memoryCache">
        <providers>
            <add name="memoryCache" type="CustomMemoryCachingProvider, App_Code"/>

 

The above implementation samples how you can easily customize the caching providers to include additional information for objects being stored in cache. You can also include additional information in the key.

2 comments

Leave a comment
  1. KingKong Feb 05, 2010
    Really cool and easy for setup, just copy and paste.
  2. Florian Feb 05, 2010
    Great stuff! Thank you very much for sharing!

    Leave a comment