Wednesday 23 August 2017

Invalidate the Site or Page Cache In EPiServer

Background


Recently I was asked for a code snippet to manually invalidate site and individual page cache in EPiServer.

My first impression was why you want to go out of the way to manually invalidate site or a page cache when EPiServer automatically takes cares of it in a brilliant way?. It's not something you do in your daily EPiServer development routine, and being a big believer of "No such thing as a stupid question", it got me curious as I had some idea about doing this in theory but never actually get a chance to write a code to invalidate site or page cache.

The idea was to stress test load balancing environment where EPiserver event providers are doing the cache invalidation job but to disrupt their job through an invalidation cache web service. Later create a proof of concept in load balancing environment where EPiserver events providers for invalidating cache are disabled and on published event in CMS, a code gets triggers manually invalidation cache on site or page.


Solution


Invalidate the cache for EPiServer site on a Server


Episerver 7
EPiServer.CacheManager.Clear();

Episerver 6
EPiServer.DataFactoryCache.Clear();

A small Webservice to invalidate site cache.

public void ProcessRequest (HttpContext context)
 {
  EPiServer.CacheManager.Clear();
        context.Response.ContentType = "text/plain";
        context.Response.Write("Ok, site cache cleared.");
    }


Invalidate the cache for a specific EPiServer page


Episerver 9.9+
var contentCacheRemover = ServiceLocator.Current.GetInstance<EPiServer.IContentCacheRemover>();
            contentCacheRemover.Remove(ContentReference.StartPage);

Episerver 6
DataFactoryCache.RemovePage(ContentReference.StartPage);

Note: Thanks to Wałdis Iljuczonok for updating me to the latest Episerver public API's as a replacement of DataFactoryCache

About the Author

Adnan Zameer, Lead Developer at Optimizley UK, is a certified Microsoft professional, specializing in web app architecture. His expertise includes Optimizley CMS and Azure, showcasing proficiency in crafting robust and efficient solutions.

0 comments :

Post a Comment