Sitecore Custom Cache implementation
To maintain some value in cache we can customize Sitecore cache by using Sitecore.Caching name space and classes. As Sitecore.Caching.CustomCache is an abstract class, you cannot directly instantiate it, but you can inherit from it.
namespace Rbm.Feature.SharjeelsWork.CacheManager
{
public class RbmCustomCache :
Sitecore.Caching.CustomCache
{
public
RbmCustomCache(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
Create a static Cache Manager to “manage” your CustomCache. This manager will instantiate the CustomCache object in the constructor.
namespace RBM.Feature.SharjeelsWork.CacheManager
{
using Sitecore;
public class RbmCacheManager
{
private static readonly RbmCustomCache Cache;
static RbmCacheManager()
{
Cache = new RbmCustomCache("RbmCustomCache",
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);
}
}
}
Code to set and get cache value.
RbmCacheManager.SetCache("rbmData", "RbmValue1");
string cachedData =
RbmCacheManager.GetCache("rbmData");
RbmCacheManager.SetCache("rbmData", "RbmValue2");
cachedData = RbmCacheManager.GetCache("rbmData");
In this way you can set and get cache value in your sitecore project.
Happy Learning!!!
Comments
Post a Comment