Custom Caching In Sitecore

Sitecore has a great caching mechanism,   in this post I will explain how you can implement custom caching with Sitecore for our custom objects to have better performance.

I have Sitecore Setting item, it contains the path for the website navigations.

settings

I am using glass mapper to cast settings item to my class:

public class SettingsModel : BaseItem
{
[SitecoreField("Top Navigation")]
public virtual TopNavigationFolderModel TopNavigation { get; set; }

[SitecoreField("Footer Navigation")]
public virtual FooterNavigationFolderModel FooterNavigation { get; set; }

[SitecoreField("Main Navigation")]
public virtual MainNavigationFolderModel MainNavigation { get; set; }

[SitecoreField("Mobile Navigation")]
public virtual MobileNavigationFolderModel MobileNavigation { get; set; }
}

I’m using MVC, my controller has 4 action methods for each view :

nav

for each  method in my controller i am getting settings item and then casting it to SettingsModel class, and there is no need for that so i am using custom Sitecore cache to cache SettingsModel object.

Implementation :

Create SiteCache Cache Class :

public class SiteCache : CustomCache
{
public SiteCache(string name, long maxSize) : base(name, maxSize)
{
}
public void SetObject(string key, object value)
{
base.SetObject(key, value, Sitecore.Reflection.TypeUtil.SizeOfObject());
}
public new object GetObject(object key)
{
return base.GetObject(key);
}
}

Note : The above code will work with Sitecore 8.1 (all revisions) , Sitecore Changed the caching APIs in  Sitecore 8.2,  so for that change you can use the following code for Sitecache :

public class SiteCache : CustomCache
 {
 public SiteCache(string name, long maxSize) : base(name, maxSize)
 {
 }
 public void AddCacheObject(string key, object value)
 {
 InnerCache.Add(key, value);
 }
 public object GetCacheObject(string key)
 {
 if (!InnerCache.ContainsKey(key)) return null;
 return InnerCache.GetValue(key);
 }
 }

CacheManager Class :

Create CacheManager class to initiate SiteCache Class and pass for it the cache key name and  the size. I added them to the config file :

<setting name="MySite.SitecoreFramework.Cache.CacheSize" value="5MB"/>
public class CacheManager
{
private static readonly SiteCache Cache = new SiteCache(
"MySite.SitecoreFramework.Cache.SiteCache",
StringUtil.ParseSizeString(Sitecore.Configuration.Settings.GetSetting("MySite.SitecoreFramework.Cache.CacheSize", "5MB")));

public static object Get(string key)
{
return Cache.GetObject(key);
}

public static void Set(string key, object value)
{
Cache.SetObject(key, value);
}
public void ClearCache(object sender, EventArgs args)
{
Cache.Clear();
}
}

GetSettingsItem Method:

I created method to get SettingsModel object from the cache if exists, otherwise will get the object and cache it again :

public static SettingsModel GetSettingsItem()
{
var siteSettings = CacheManager.Get("SettingsItemKey");
if (siteSettings == null)
{
var contextService = new SitecoreContext();
var currentSitePath = Sitecore.Context.Site.ContentStartPath;
var query = string.Format("{0}{1}", currentSitePath, "/*[@@templatename='Settings']");
var settingsItem = Sitecore.Context.Database.SelectSingleItem(query);
var settingModel = contextService.Cast<SettingsModel>(settingsItem);
CacheManager.Set("SettingsItemKey", settingModel); 
} 
return (SettingsModel)siteSettings;
 }

after that open the cache.aspx page :

http://yourwebstie/sitecore/admin/cache.aspx

and you will find the key there :

cache

Clear Caching :

Now we need to clear caching on publish event, I’m calling ClearCache Method from ChacheManager Class :

<events>
<event name="publish:end">
<handler type="MySite.SitecoreFramework.Cache.CacheManager, MySite.SitecoreFramework" method="ClearCache"/>
</event>
<event name="publish:end:remote">
<handler type="MySite.SitecoreFramework.Cache.CacheManager, MySite.SitecoreFramework" method="ClearCache"/>
</event>
</events>
Advertisements

5 thoughts on “Custom Caching In Sitecore

    1. That is awesome! this is just example, and i used here glass mapper, the main idea is you can use Sitecore caching with any object not necessary with glass mapper. Thanks Jammy!

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s