We are creating a report page where we will display the cached data source / presentation . A button will be there to remove selected cached item.
Which Sitecore Api can we use to retrieve the list of cached items?
Sitecore Stack Exchange is a question and answer site for developers and end users of the Sitecore CMS and multichannel marketing software. It only takes a minute to sign up.
Sign up to join this communityAfter searching on Sitecore API's i got Sitecore.Caching.CacheManager
class which has method GetHtmlCache
and it returns Sitecore.Caching.HtmlCache
.
With the below code I can retrieve the list of cache items.
List<Sitecore.Caching.Cache.CacheEntry> cacheList = new List<Sitecore.Caching.Cache.CacheEntry>();
Sitecore.Sites.SiteContext siteContext = Sitecore.Sites.SiteContext.GetSite("SiteName");
if (siteContext != null)
{
Sitecore.Caching.HtmlCache htmlCahe = Sitecore.Caching.CacheManager.GetHtmlCache(siteContext);
var cacheKeys = htmlCahe.InnerCache.GetCacheKeys();
foreach (var sKey in cacheKeys)
{
Sitecore.Caching.Cache.CacheEntry entry = htmlCahe.InnerCache.GetEntry(sKey, false);
cacheList.Add(entry);
}
}
There are some good introductory articles about Sitecore caching, e.g. this one.
Generally you should use CacheManager
to work with cache:
HtmlCache cache = Sitecore.Caching.CacheManager.GetHtmlCache(Sitecore.Context.Site);
cache.Remove(cacheKey);
Other methods related to removing items from cache are RemovePrefix()
and RemoveKeysContaining()
.