3

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?

2 Answers 2

4

After 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);

                }

            }
0

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().

1
  • Nice article, it has good information on caching.
    – Abhi
    Nov 28, 2017 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.