LibrarySites.Banner

Sitecore Output Cache Clearing Optimization (9/8): Schedule Cache Clearing

This post-final entry about scheduling cache processing is part of a series of blog posts about a custom approach to clearing output caches for managed sites with the Sitecore ASP.NET web Content Management System (CMS) and Experience Platform (XP). For more information, see the Resources section at the end of this blog post.

The cache clearing process described in this series may not clear output caches for sites that specify an outputCacheMinimimInterval property. The objective is to increase performance by specifying a minimum interval between cache clearing events on the assumption that subsequent events will clear the caches again anyway. What happens if such a subsequent event does not occur?

It would be nice to schedule something to clear the cache after the interval expires. While Sitecore provides some features, scheduling processing in ASP.NET is tricky. There is probably a way to make something run at a specific time, but I do not know of anything. Sitecore does not provide an API to schedule something to run at a specific time, which could be unreliable because ASP.NET might not be up at that instance (although that fact is irrelevant in this case).

We can use a scheduled task for this, but with some considerations:

  • We cannot schedule processing for a specific instant. Rather, after we create our task definition, the next run of the database agent should determine whether to invoke it.
  • In the case of multiple concurrent publishing operations, we probably do not want to schedule multiple cache clearing operations.
  • Multiple content management and content delivery instances may need to schedule tasks, so we need to designate the instance that should run each.

One of the challenges involves passing custom parameters from our event handler to the scheduled task. We can create a custom template with a field for this value and use serialization.

First, I created the ProcessOutputCaches class to invoke the clearOutputCaches pipeline at some point in the future. If the value of the field named Instance differs from the name of the instance, the the Process() method in this class does nothing (a different instance scheduled this task). Otherwise, the Process() method deseraizlies XML in the OutputCacheClearingOptions field of that item to create a OutputCacheClearingOptions object that it passes to the ClearOutputCaches() method of a new OutputCacheClearer object. Finally, if the Auto Remove field in the scheduled definition item is checked, it removes that schedule definition item.

I modified the CheckInterval processor in the clearOutputCaches pipeline to create schedule definition items as required.

I created the /sitecore/system/Tasks/Commands/ProcessOutputCaches item using the System/Tasks/Command data template. IN the type field I specified Sitecore.Sharedsource.Tasks.ProcessOutputCaches,Sitecore.Sharedsource, with Process in the Method field.

This solution requires the new data User Defined/Tasks/Schedule/ProcessOutputCaches data template that inherits from the System/Tasks/Schedule data template used to define schedules by default. The ProcessOutputCaches data template defines the Instance field to specify the instance that scheduled and should process the task as well as the OutputCacheClearingOptions that contains serialized cache clearing options.

There are at least two issues with the default DatabaseAgent that processes scheduled tasks:

  • It does not run in the web database by default.
  • It will only process tasks with definition items based on the data template provided by Sitecore

Therefore, we have to implement our own DatabaseAgent. Unfortunately, this requires overriding enough private methods that it did not seem to make sense to inherit from the default implementation.

You may have tasks defined in the Master database that are irrelevant or even cause errors when they appear in publishing target databases. You can use publishing restrictions to prevent tasks defined in the Master database from appearing in the publishing target database.

Resources