Sitecore Caching


Sitecore uses caching to improve system performance and response time.

Cache Basics

Sitecore uses various caches to store data, rendered presentation logic and other information in memory in order to improve performance and response time. A cache is a dictionary that keeps track of when each entry is accessed. The data that is stored for each entry depends on the cache. How the cache is used also depends on the cache.

Sitecore Caches

The following is a list of caches that come with Sitecore. This is not an exhaustive list. New caches can be added at any time. Modules may add caches.

These are caches that inherit from Sitecore.Caching.CustomCache class. There are many more caches that are instances of Sitecore.Caching.Cache.

NamePurpose
AccessResultCacheMinimize the number of database reads needed to retrieve access rights.
DataCacheMinimize the number of times Sitecore has to read item meta-data and parent/child relationship information from the database.

Each Sitecore database as its own data cache.
DeviceItemsCacheEfficiently access items that represent the devices defined in Sitecore databases.
HtmlCacheMinimize the number of times Sitecore has to process renderings.
ItemCacheMinimize the number of database reads needed to retrieve item data.

Each Sitecore database as its own item cache.
ItemPathsCacheMinimize the number of times Sitecore has to resolve an item ID to a path

Each Sitecore database as its own items path cache.
PathCacheMinimize the number of times Sitecore has to resolve a path to an item ID.
RegistryCacheMinimizes the number of times Sitecore has to access its registry.
RuleCacheMinimizes the number of database reads needed to retrieve rules.
StandardValuesCacheMinimizes the number of database reads needed to retrieve standard values

Each Sitecore database as its own standard values cache.
ViewStateCacheMinimizes the number of times Sitecore has to read view state from permanent storage.
XslCacheMinimizes the number of times Sitecore has to process XSL renderings.
FieldReaderCacheMinimizes the number of database reads needed to retrieve field values.

Cache Monitoring

Sitecore provides a basic cache monitoring script. It allows you view a list of the caches. For each cache you can see the number of records, the size of the data being cached and other information. You also have the ability to clear all of the caches.

http://[host]/sitecore/admin/cache.aspx

Sitecore Rocks provides more powerful cache monitoring features. In addition to displaying cache sizes and providing the ability to clear all caches, it allows you to clear individual caches, as well as to remove individual records from a cache.

Adding a Cache

Caches can be created as needed in your code.

Consider implementing a custom cache. Custom caches are strongly-typed caches. They are easier to work with and to keep track of.

<a name=creating_the_cache”“>Creating the Cache</a>

Creating a cache consists of creating a new instance of Sitecore.Caching.Cache. When the instance is created the cache is automatically registered with the system. This means that Sitecore recognizes the cache and can control it.

var mycache = new Sitecore.Caching.Cache("test cache", 1024);

For Sitecore version 9.0 or later, create a cache instance using the GetNamedInstance method of CacheManager static class.

ICache<string> mycache = Sitecore.Caching.CacheManager.GetNamedInstance("test cache", 1024, true);

Referencing the Cache

A cache is accessed by name using the Cache Manager.

var mycache = Sitecore.Caching.CacheManager.FindCacheByName("test cache");

Adding a Value to the Cache

A value is added to the cache using the Add method.

var mycache = Sitecore.Caching.CacheManager.FindCacheByName("test cache");
mycache.Add("name", "value"); 

Reading a Value from the Cache

A value is read from the cache using the GetValue method. Reading a value notifies that the value has been read, which resets the expiration timer.

var mycache = Sitecore.Caching.CacheManager.FindCacheByName("test cache");
var value = mycache.GetValue("name");

Removing a Value from the Cache

A value is removed from the cache using the Remove method.

var mycache = Sitecore.Caching.CacheManager.FindCacheByName("test cache");
mycache.Remove("name");

Clearing the Cache

A cache can be cleared using the Clear method.

var mycache = Sitecore.Caching.CacheManager.FindCacheByName("test cache");
mycache.Clear();

Implementing a Custom Cache

A custom cache is a strongly-typed cache. These types of caches are easier to work with. In in other regards a custom cache works the same way as other caches.

Custom Cache Example

The following is an example of a custom cache and a manager class for accessing the cache.

public class AbbreviationCache : CustomCache<string>
{
    public AbbreviationCache(string name, long maxSize) : base(name, maxSize) {  }

    public void SetAbbreviation(string key, string value)
    {
        base.SetString(key, value);
    }
    public string GetAbbreviation(string key)
    {
        return base.GetString(key);
    }
}

public class AbbreviationCacheManager
{
    private static AbbreviationCache _abbreviationCache;

    public static AbbreviationCache GetAbbreviationCache()
    {
        if (_abbreviationCache == null)
        {
            var name = "AbbreviationCache";
            var size = Sitecore.Configuration.Settings.Caching.SmallCacheSize;
            _abbreviationCache = new AbbreviationCache(name, size);
        }
        return _abbreviationCache;
    }
}

The following is an example of how to use the custom cache.

var cache = Testing.Caching.AbbreviationCacheManager.GetAbbreviationCache();
cache.SetAbbreviation("adc", "Adam David Conn");