Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
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.
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(...) |
To implement a custom cache the interface EPiServer.BaseLibrary.IRuntimeCache has to be implemented.
Example of an 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);
}
}
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:
[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){}
}
Last updated: Mar 25, 2013