This content is archived. See latest version here

Last updated: Mar 25 2013

Introduction

The purpose of the pluggable cache is to allow developers to replace the runtime cache used by EPiServer CMS with their own custom implementation.

With EPiServer CMS it is possible for developers to replace the cache functionality used by EPiServer CMS with their own custom developed plug-in.

Using the pluggable cache functionality in EPiServer CMS

The class EPiServer.CacheManager has been expanded with a RuntimeCache property to allow developers to access an instance of the runtime cache. In addition, the following static helper functions have been added to this class:

Original functionNew function
HttpRuntime.Cache.Insert(...) CacheManager.RuntimeCacheInsert(...)
HttpRuntime.Cache.Add(...) CacheManager.RuntimeCacheAdd(...)
HttpRuntime.Cache.Remove(...) CacheManager.RuntimeCacheRemove(...)
HttpRuntime.Cache.Get(...) CacheManager.RuntimeCacheGet(...)

Implementing a custom cache

To implement a custom cache the interface EPiServer.BaseLibrary.IRuntimeCache has to be implemented.

Note that when several modules uses the same cache, each module is going to create an instance of the cache object. If you intend for them to use the same cache you have to implement a singleton pattern for it.

Example of an implementation of “logging cache”:

C#
using System;
using System.Web;
using System.Web.Caching;
using EPiServer;
using EPiServer.BaseLibrary;
using log4net;

public class LoggedHttpRuntimeCache : IRuntimeCache
{
    public void Clear()
    {
        // ToDo: Provide implementation for this function
    }

    private static readonly ILog log =
        LogManager.GetLogger(typeof(LoggedHttpRuntimeCache));

    public void Insert(string key, object value, CacheDependency dependencies,
        DateTime absoluteExpiration, TimeSpan slidingExpiration,
        CacheItemPriority priority)
    {
        Insert(key, value, dependencies, absoluteExpiration,
            slidingExpiration, priority, null);
    }

    public object Add(string key, object value, CacheDependency dependencies,
        DateTime absoluteExpiration, TimeSpan slidingExpiration,
       CacheItemPriority priority)
    {
        return Add(key, value, dependencies,
            absoluteExpiration, slidingExpiration, priority,
            null);
    }

    public void Remove(string key)
    {
        log.Debug("LogRemove:" + key);
        HttpRuntime.Cache.Remove(key);
    }

    public object Get(string key)
    {
        log.Debug("LogGet:" + key);
        return HttpRuntime.Cache.Get(key);
    }

    public object Add(string key, object value, CacheDependency dependencies,
                      DateTime absoluteExpiration, TimeSpan slidingExpiration,
                      CacheItemPriority priority,
                      CacheItemRemovedCallback onRemoveCallback)
    {
        log.Debug("LogAdd:" + key);
        return HttpRuntime.Cache.Add(key, value, dependencies,
            absoluteExpiration, slidingExpiration, priority,
            onRemoveCallback);
    }

    public void Insert(string key, object value, CacheDependency dependencies,
                       DateTime absoluteExpiration, TimeSpan slidingExpiration,
                       CacheItemPriority priority,
                       CacheItemRemovedCallback onRemoveCallback)
    {
        log.Debug("LogInsert:" + key);
        HttpRuntime.Cache.Insert(key, value, dependencies, absoluteExpiration,
            slidingExpiration, priority, onRemoveCallback);
    }

}

Plugging in a custom cache for EPiServer CMS

To plug in the custom cache you have to add the following initialization module replacing the implementation of IRuntimeCache interface in your implementation of your site:

C#
[ModuleDependency(typeof(ServiceContainerInitialization))]
public class InitializeLoggedCache : IConfigurableModule
{
public void ConfigureContainer(ServiceConfigurationContext context)
{
context.Container.Configure(
x=> {
x.For<IRuntimeCache>()
.Use<LoggedHttpRuntimeCache>();
}
);
} public void Initialize(EPiServer.Framework.Initialization.InitializationEngine context){}
public void Preload(string[] parameters){}
public void Uninitialize(EPiServer.Framework.Initialization.InitializationEngine context){}
}

Do you have feedback on this documentation? Send an email to documentation@episerver.com. For development-related questions and discussions, refer to our Forums on https://world.episerver.com/forum/