Return to doc.sitecore.com

Valid for Sitecore 5.3, 5.2
Clearing cache programmatically

Clearing cache operation will be a good example to illustrate the principle of the new approach to cache implementation.

Consider the situation when you wish to clear the cache programmatically on an event. You can use the following code sample in this case:

 HtmlCache cache = CacheManager.GetHtmlCache(Context.Site);

  if (cache != null) {
    cache.Clear();
  }

 

The example above is intended for clearing the HTML cache. You also can clear the other caches in a similar way. Just replace GetHtmlCache method with an appropriate one – GetXslCache, GetRegistryCache and so on.

You can also use CacheManager.ClearAll() method to clear all cache.

Please, refer to the Cache article for more explanations on cache types.