ASP.Net Caching and Sitecore (HttpContext.Current.Cache vs. HttpRuntime.Cache)

This post is about using ASP.Net caching with sitecore, separate from the item caching that sitecore itself provides. (Sitecore Caching)

We are all possibly aware of the way to use the cache accessible using the HttpContext.Current.Cache property. In the scenario of Sitecore, we might also want to add the functionality to clear all cached data on any publish. Sitecore itself does clear all the data that it caches using the following event at the end of the publish process:


<event name="publish:end">
 <handler type="Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel" method="ClearCache">
 <sites hint="list">
 <site>website</site>
 </sites>
 </handler>
 </event>
 <event name="publish:end:remote">
 <handler type="Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel" method="ClearCache">
 <sites hint="list">
 <site>website</site>
 </sites>
 </handler>
 </event>

But this will only clear the keys which Sitecore itself created.

If there was any data which you have manually cached in your site, they will remain unaffected.

Say we used the following to insert data into the cache:

HttpContext.Current.Cache.Insert("Key", "Value");

And then to the publish pipeline we add the following:

<pipelines>
 <publishItem help="Processors should derive from Sitecore.Publishing.Pipelines.PublishItem.PublishItemProcessor">
 <processor type="MySite.Classes.ClearHttpCache, MySite" />
 </publishItem>
</pipelines>

Which would look something like this:

using Sitecore.Publishing.Pipelines.PublishItem;

namespace MySite.Classes
{
 public class ClearHttpCache : PublishItemProcessor
 {
 public override void Process(PublishItemContext context)
 {
 var cacheEnumerator = HttpContext.Current.Cache.GetEnumerator();
 while (cacheEnumerator.MoveNext())
 HttpContext.Current.Cache.Remove(cacheEnumerator.Key.ToString());
 }
 }
}

When we go ahead and implement this, you’ll see that this still doesn’t work!
Here comes the distinction between HttpContext.Current.Cache and HttpRuntime.Cache.

The cache is stored in the session established for the front end site, while we are attempting to clear this cache in a session established for the sitecore editor (a different session). And HttpContext.Current.Cache is not shared across instances. HttpContext.Current.Cache internally does use HttpRuntime.Cache which infact is shared across instances for a given app domain.

Using HttpRuntime.Cache is the key here which will enable us to achieve this functionality. You could define a prefix for the keys which you would want cleared out on publish if you wouldn’t want all the keys in cache cleared.

So you’d use the following:

// Insert
HttpRuntime.Cache.Insert("Key", "Value");

// Access
var cacheValue = HttpRuntime.Cache["Key"];

And to clear the cache – on publish:

using Sitecore.Publishing.Pipelines.PublishItem;

namespace MySite.Classes
{
 public class ClearHttpCache : PublishItemProcessor
 {
 public override void Process(PublishItemContext context)
 {
 var cacheEnumerator = HttpRuntime.Cache.GetEnumerator();
 while (cacheEnumerator.MoveNext())
 HttpRuntime.Cache.Remove(cacheEnumerator.Key.ToString());
 }
 }
}
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 )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s