Sitecore

Making Use of Sitecore Custom Cache

By September 3, 2015 No Comments

This article will describe another useful Sitecore functionality, creating a Sitecore custom cache object and using it efficiently.

Among other types of caches that Sitecore supports, ā€œSitecore.Caching.CustomCacheā€ is a method which allows us to inherit and to easily
configure based on our needs.

In one of my previous posts, I was presenting how to use the Serialization in order to track important items changes. One improvement to that was to use a Cached Serialization Settings item instead of getting the configuration item from the database on each item save.

There are some settings which we can configure for such a custom cache: the max size of the cache (configured in my example to 1 Mb), time to live for the cache (this can be configured for each item added in the cache), or the cache name.

Check this sample code:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using Sitecore;  
using Sitecore.Caching;  
using Sitecore.Configuration;  
using Sitecore.Data.Items;

namespace Custom.Serialization  
{
    public class SerializationSettings: CustomCache
    {
        public static SerializationSettings SerializationCache
        {
            get { return cache ?? (cache = new SerializationSettings()); }
        }

        private SerializationSettings() : base("serialization-settings-cache", MAX_SIZE)
        {}

        private readonly TimeSpan timeToLive = new TimeSpan(0, 0, 0, 5400);
        private static readonly long MAX_SIZE = 1048576L;
        private readonly int avgItemSize = Settings.Caching.AverageItemSize;
        private static SerializationSettings cache;
        private const string SERIALIZATION_CACHE_PATH = "/sitecore/content/Settings/serialization-settings";

        public Item GetItem
        {
            get
            {
                Item item = SerializationCache.Get();
                if (item == null)
                {
                    SerializationCache.Add();
                }
                return item ?? SerializationCache.Get();
            }
        }

        public void Add()
        {
            Item serializationSettingsItem = Context.ContentDatabase.GetItem(SERIALIZATION_CACHE_PATH);
            if (serializationSettingsItem != null)
            {
                InnerCache.Add(CreateKey(SERIALIZATION_CACHE_PATH), serializationSettingsItem, avgItemSize, timeToLive);
            }
        }

        public Item Get()
        {
            return (Item)InnerCache.GetValue(CreateKey(SERIALIZATION_CACHE_PATH));
        }

        private string CreateKey(string path)
        {
            return Context.Database + "_" + path;
        }
    }
}

The code is pretty much self explanatory, but there are some things worth mentioning here: Iā€™m using a Cache key, composed of the Sitecore Context Database and the serialization settings path, so it will cache different items for Master(Preview) and Web.

The Get() and Add() methods are using the *InnerCache *class, which is inherited from the *CustomCache *class.

So, referring to my previous blog post, instead of having the Settings fetched from the database:

private const string SERIALIZATION_SETTINGS_PATH = "/sitecore/content/Settings/serialization-settings";

Item serializationSettings = Context.ContentDatabase.GetItem(SERIALIZATION_SETTINGS_PATH);  

We can now call the SerializationSettings class:

Item serializationSettings = SerializationSettings.SerializationCache.GetItem;  

The cool thing about it is that after the first use, this cache will appear on http://sitecore_instance/sitecore/admin/cache.aspx so you can clear it whenever necessary:

caches

Adrian Iorgu

Author Adrian Iorgu

Results-oriented and self-motivated lead engineer with a focus on delivering high-quality code and products in high traffic environments. Sitecore MVP 2016 & 2017

More posts by Adrian Iorgu

Leave a Reply