Pluggable Runtime Cache

Product version:

EPiServer CMS 5 R1

Document version:

1.0

Document creation date:

23-11-2006

Purpose

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


Table of Contents

Introduction

Using the Pluggable Cache Functionality in EPiServer

Implementing a Custom Cache

Plug in a Custom Cache for EPiServer CMS 5


Introduction

In EPiServer CMS 5 it is possible for partner developers to replace the cache functionality used by EPiServer CMS with their own customized plug-ins.

Using the Pluggable Cache Functionality in EPiServer CMS 5

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

Original function

New 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 must be implemented.

Remarks:

When several modules use  the same cache, each module will create an instance of the cache object. If you intend them to use the same cache, you must implement a singleton pattern for it.

Example of implementation of  “logging cache”.

using System;

using System.Web;

using System.Web.Caching;

using EPiServer;

using EPiServer.BaseLibrary;

using log4net;

 

public class LoggedHttpRuntimeCache : IRuntimeCache

{

    private static readonly ILog log =

        LogManager.GetLogger(typeof(LoggedHttpRuntimeCache));

    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);

    }

    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 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);

    }

    #endregion

}  

Plug in a Custom Cache for EPiServer CMS

Plug in the custom cache by adding the following rows to the Web.Config file of your site:

<configuration>

  <configSections>

    <section name="episerver.baseLibrary"

      allowDefinition="MachineToApplication"

      allowLocation="false"      

      type="EPiServer.BaseLibrary.ConfigurationHandler,EPiServer.BaseLibrary" />

  </configSections>

.

.

.

  <episerver.baseLibrary>

    <classFactories>

      <add

        type="EPiServer.Implementation.DefaultBaseLibraryFactory, EPiServer.Implementation"

        id="EPiServerRuntime" >

        <assignStatic

            type="EPiServer.BaseLibrary.ClassFactory"

            property="Instance" />

        <register

            type="EPiServer.BaseLibrary.IRuntimeCache, EPiServer.BaseLibrary"

            mappedType="EPiServerSample.CustomCache.LoggedHttpRuntimeCache, EPiServerSample" />

      </add>

    </classFactories>

  </episerver.baseLibrary>