When to clear your custom caches in Sitecore 8

If you are leveraging any in-memory caching in your Sitecore web application (e.g. using System.Runtime.Caching.MemoryCache) to reduce the computational load on the server, and the cached data is created using some data which comes from Sitecore, you'll need to ensure the in-memory cache is cleared at the correct times in the application data's lifecycle. One example of the need for such caching might be when a rendering cannot be HTML cached (due to some data within it being unique to the user, e.g. fetched from some domain data repository) but some part of the view model (which the rendering uses) simply depends on some items within Sitecore, based on some computational logic (so why compute this over and over again?)

You can take a lead from Sitecore's out-of-the-box HtmlCacheClearer event handler, and ensure you too have a handler which clears your in-memory caches when a publish happens, to ensure that you use the most recent data from Sitecore to build up your cached values. This would be a very good start, however there's another case which you might not have thought of.

If the cached data relies on an index, another layer of indirection is introduced - not only does a publish need to have taken place, but - depending on your indexing strategies - you may have to wait for any incremental index updates to complete. If your cached data relies on an index, and the following sequence of events happen, you will be out of luck:

 

- Content editor changes item in Sitecore

- Changed item gets published. This clears your custom cache too

- A user requests your page, and the in-memory cache is then filled with stale data (because it relies on an index, which has not yet been updated)

- The index finally gets updated with the change that the content editor made

- A user requests your page, and the in-memory cache *still* contains stale data

 

Fortunately there is a simple solution. Additionally clear your custom cache when a partial index update occurs. Specifically, subscribe a handler to the indexing:end event. The new sequence of events is then:

 

- Content editor changes item in Sitecore

- Changed item gets published. This clears your custom cache too

- A user requests your page, and the in-memory cache is then filled with stale data (because it relies on an index, which has not yet been updated)

- The index finally gets updated with the change that the content editor made. This clears your custom cache too

- A user requests your page, and the in-memory cache is then filled with fresh data :)

 

As always, your solution may have unique needs, so balance this advice against the actual behaviour of your indexing strategies - e.g. if you don't use partial index updating there may be no need for this (or alternatively if the indexes get updated a lot, consider the impact of the frequent burning of your custom caches.) Always set up a simple test in a development environment to demonstrate the need for handling such circumstances :)

 


By James at 23 Aug 2016, 21:01 PM


Comments

Post a comment