Sitecore comes stocked with a custom caching mechanism in its Sitecore.Caching namespace. Unfortunately, there is not much documentation on it, but this post will hopefully shed some light on this mysterious, yet useful object known as CustomCache.
To implement, you will need 3 things:
- Your own CustomCache class
- A CacheManager class
- The code that will be using the cache
Your own Custom Cache class
Because Sitecore.Caching.CustomCache is an abstract class, you cannot directly instantiate it, but you can inherit from it.
namespace Sitecore.Custom
{
public class MyCustomCache : Caching.CustomCache
{
public MyCustomCache(string name, long maxSize) : base(name, maxSize)
{
}
new public void SetString(string key, string value)
{
base.SetString(key, value);
}
new public string GetString(string key)
{
return base.GetString(key);
}
}
}
Cache Manager
You will need a static Cache Manager to “manage” your CustomCache. This manager will instantiate the CustomCache object in the constructor. This is where you will give it the name and the size – both of which will show up in under /sitecore/admin/cache.aspx which you will be able to clear.
namespace Sitecore.Custom
{
public static class CacheManager
{
private static readonly MyCustomCache Cache;
static CacheManager()
{
Cache = new MyCustomCache("MyCustomCache",
StringUtil.ParseSizeString("10KB"));
}
public static string GetCache(string key)
{
return Cache.GetString(key);
}
public static void SetCache(string key,string value)
{
Cache.SetString(key, value);
}
}
}
Your code that calls it
You can use the code below to set and get your cached values.
CacheManager.SetCache("myData","myDataValue");
string myData = CacheManager.GetCache("myData");
So why use this as opposed to the .NET cache out of the box? For me, it’s the simple convenience of having it clear anytime I do a Sitecore publish operation.
That’s all there is to it.

Assuming your putting a custom key and custom object into the cache, how do you implement a dependency to the items that object represents?
Hey Eric,
Not sure if there is something internal to Sitecore that you can override or if you have to code your own.
Will look into this for you and get back to you. Excellent question though!
Eric,
This is working perfectly for me, but I have one issue. I’m not sure how to clear the custom cache on publish. You state that “it clear[s] anytime I do a Sitecore publish operation.” However, I am not finding this to be the case. I see it in the /sitecore/admin/cache.aspx list, and so I can clear it manually. But I don’t see how to clear it in the publish:end event.
Any ideas?
Josh
What version of Sitecore are you running?
If you are using the staging module, you need to set the cache setting to full instead of partial.
Hey Josh,
add an event handler like this may be this will help
then add the above handler under in web.config
and implement your logic in “OnPublishEnd” to clear your cache keys
I used the same sort of approach but adding in generics and also have a few more twists. Have a look and hope it helps;
http://cjgiddings.wordpress.com/2012/02/03/sitecore-caching-utility/