Introduction
The purpose of the pluggable cache is to allow developers to replace the runtime cache used by EPiServer with their own custom implementation.
With EPiServer CMS it is possible for developers to replace the cache functionality used by EPiServer with their own custom developed plug-in.
Using the pluggable cache functionality in EPiServer
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 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 has to be implemented.
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 { 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); } }
Plug-in a custom cache for EPiServer
To plug in the custom cache you have to add 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> <!-- --> <!-- Additional settings here --> <!-- --> <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> </configuration>