Implement caching by domain name

Implement caching by domain name

February 05, 2010 0 Comments

The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.

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.

progress-logo

The Progress Team

View all posts from The Progress Team on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.

Comments
Comments are disabled in preview mode.
Topics
 
 
Latest Stories in
Your Inbox
Subscribe
More From Progress
New_Mobile_Dev_Ebook_Progress_Website_Thumbail
The New Mobile Development Landscape
Download Whitepaper
 
IDC Spotlight Sitefinity Thumbnail
Choosing the Right Digital Experience Platform to Improve Business Outcomes
Download Whitepaper
 
TheFastestWayToBuildMobileAppsArtboard-2
The Fastest Way to Build Mobile Apps With Cloud Data
Watch Webinar