Skip To Content

Kentico Tip: How to Manage Scheduled Publishing of Cached Content

Using Kentico's caching features is a must for practically any website. Caching is an important part of the package that enables Kentico to hold up to the demands of enterprise customers. Additionally, when optimized, it enables the hosting of a high-traffic website using an economical Azure SQL service tier. The ability to configure cache dependencies enables the use of large intervals, so that a website can serve the same page from cache over a long period of time. With dependencies correctly configured, Kentico will reliably clear the cache of a page when any of its dependencies are modified.

The Challenge: Using Caching with Scheduled Publishing

However, a potential issue arises when long cache intervals are used in combination with the scheduled publishing feature. Kentico allows the publishing of a page to be scheduled with a “Publish From” time and a “Publish To” time. However, if using long interval caching, you may discover that a new version of a page doesn’t automatically become published when its “Publish From” time is reached. Likewise, the content doesn’t automatically become unpublished when the “Publish To” time is hit. This is likely because the original page has stayed cached despite the scheduled publishing.

This challenge can certainly be solved by using a much smaller cache interval. But, if a long interval is needed, or near real-time publishing is required, you can still achieve the best of both worlds by using a little bit of custom code and Kentico’s extensibility.

One important thing to understand is that the way Kentico handles the “Publish From” time and the “Publish To” time functionality is different, so the solution for each will need to be handled differently. Here’s how to solve both these challenges:

Managing the “Publish From” time

When using content scheduling and workflow together, it is important to realize that the current published version of a page is not replaced until the new version’s “Publish From” time is reached. For example, if you want to change the address on your contact page from Boston to Seattle tomorrow morning, you would make your content edits to the page, and then set the “Publish From” time to the morning.

After publishing the page, two versions of the page exist: the Boston version, which is still published, and the Seattle version, which will be published in the morning. To manage this, Kentico provides the “Content publishing” scheduled task, which runs every minute to check the “Publish From” time of pages. When it finds a page that needs to be published, it transitions the new page to the published state. In our example, the scheduled task would transition the Seattle version of the page to the published state, when the “Published From” time is hit. However, the Boston version of the page may still be in the cache and served to end users.

Fortunately, when the page is transitioned to the published state, a global event is fired. This allows you, through the use of some custom code, to check the scheduled dates of your site pages, and invalidate the page’s cache whenever a new published page is detected.

Here’s a simple example:

private void PublishPage_After(object sender, WorkflowEventArgs e)

{

    var publishedPage = e.PublishedDocument;

    if (publishedPage != null)

    {

        var publishedFromDate = publishedPage.DocumentPublishFrom;

        var publishedToDate = publishedPage.DocumentPublishTo;

        var now = DateTime.Now;




        if ((publishedFromDate != DateTime.MinValue) && (now >= publishedFromDate) && (now < publishedToDate))

        {

            publishedPage.ClearCache();

            var successMessage = $"A page has been published via scheduling. Cache cleared.\r\n{publishedPage.NodeAliasPath}";

            EventLogProvider.LogEvent(EventType.INFORMATION, MODULE_NAME, "PublishedPageDetected", eventDescription: successMessage);

        }

    }

}

Managing the “Publish To” time

After learning what happens when a page reaches its “Publish From” time, you might be hoping that another workflow event will fire when the “Publish To” time is reached. Unfortunately, that’s not the case — when a “Publish To” time is reached, a workflow transition does not occur. The page is simply filtered out by queries for published content.

However, unlike the “Publish From” value, the “Publish To” value is available when querying published content. This allows you to check the end-time value of a page, and even the end-time value of all dependencies at the time the cache is created. By doing so, you can adjust the cache duration so that it does not exceed the “Publish To” value of the page or any of its dependencies.

To illustrate, here’s a sample snippet of code that finds the soonest “Publish To” time from a list of tree nodes:

var nearestToTime = treeNodes.Select(n => n.DocumentPublishTo).Where(d => d >= now).Min();

var expireDuration = nearestToTime - now;

if (expireDuration < cacheDuration)

{

    cacheDuration = expireDuration;

}




cacheSettings.CacheMinutes = cacheDuration.Minutes;

That’s all there is to it! With Kentico’s extensible platform, you can maintain the efficiency of long-term caching and still reliably use features like scheduled publishing.

Have questions on how to get the most out of your Kentico implementation? Drop me a line on Twitter (@HeyMikeWills), or contact our BlueModus team of experts today.