Sitecore CacheManager

This CacheManager is used to access via code the sitecore cache, be very careful when you use it.

using Sitecore;
using Sitecore.Caching;
using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Web;

namespace Backbone.Sitecore
{
public class CacheManagerUtil
{
public static void ClearAllHtmlCaches()
{
foreach (SiteInfo info in Factory.GetSiteInfoList())
{
info.HtmlCache.Clear();
}
}
public static void ClearAllCaches()
{
Cache[] allCaches = CacheManager.GetAllCaches();
Cache[] array = allCaches;
for (int i = 0; i < array.Length; i++)
{
Cache cache = array[i];
cache.Clear();
}
}

public static void DeleteItemCache(string id)
{
Database db = Context.Database;
if (ID.IsID(id))
{
var itemID = new ID(id);
//clear data cache
db.Caches.DataCache.RemoveItemInformation(itemID);

//clear item cache
db.Caches.ItemCache.RemoveItem(itemID);

//clear standard values cache
db.Caches.StandardValuesCache.RemoveKeysContaining(itemID.ToString());

//remove path cache
db.Caches.PathCache.RemoveKeysContaining(itemID.ToString());
}
}

private Cache GetPrefetchCache(Database database)
{
foreach (Cache cache in CacheManager.GetAllCaches())
{
if (cache.Name.Contains(string.Format("Prefetch data({0})", database.Name)))
{
return cache;
}
}
return null;
}

public static void RemoveCaches(string id)
{
foreach (Cache cache in CacheManager.GetAllCaches())
{
cache.RemoveKeysContaining(id);
}
}
}
}
Advertisements

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s