Sitefinity Cache

Here's how to leverage the Sitefinity Cache Manager in your projects...super easy!

How to add the Cache Manager
public virtual ICacheManager Cache
{
    get
    {
        return SystemManager.GetCacheManager(CacheManagerInstance.Global);
    }
}

How to query the Cache
public XDocument GetXmlDocument(){
    XDocument doc = null;
    string cacheKey = "MycacheKey";
 
    if (this.Cache.Contains(cacheKey))
    {
        //Return the cached object
        return (this.Cache.GetData(cacheKey)) as XDocument;
    }
    else
    {
        //Populate the object
        doc = this.GetXmlData();
 
        //Add object to the cache
        this.Cache.Add(cacheKey,
                  doc,
                   Telerik.Microsoft.Practices.EnterpriseLibrary.Caching.CacheItemPriority.Normal,
                    null,
                    new Telerik.Microsoft.Practices.EnterpriseLibrary.Caching.Expirations.AbsoluteTime(TimeSpan.FromMinutes(this.CacheTimeoutMinutes)));
 
        //Return the object
        return doc;
    }
}


So there's clearly also methods to remove items from the cache as well, just as easy
this.Cache.Remove(cacheKey)

Enjoy!
Credit: Elena Ganeva @ telerik
Did you like this post, did I help you out, are you feeling generous

  Buy me a beer

Comments