Keeping composite pages up to date

Published: 8/19/2013, Author: Håkan Edling Categories: Tutorials, Piranha, .NET, 0 Comments

A problem in almost all systems when dealing with caching and composite pages is to invalidate the cache for a page when a related entity is update. In this post we'll look at how you could handle this manually in Piranha CMS.

In Piranha CMS all pages are cached server side, as well as leveraging browser cache on the client side. This is of course to minimize the processing needed and to keep the site as fast and efficient as possible. When a page is updated in the manager interface the page_last_published date is updated and the server cache for that particular page is invalidated causing all subsequent request to get the latest version of the page.

However, the model also contains an additional field named page_last_modified which can be modified by other entities than the page itself. One example is when updating or deleting a media file in the manager interface. By doing so all pages and posts having that media file as an attachment will be updated and invalidated from the server cache. But what if you have a custom entity, perhaps stored in a custom table in your database, that's listed on a page. How do you tell the framework that the page should be invalidated.

With the following short helper method you can make that call manually from your manager code to invalidate the page and make sure all clients requesting the page gets an up to date version.

// Touches the page and invalidates the cache.
public static void TouchPage(Guid id) {
// Update both the published and draft version in one go.
Piranha.Models.Page.Execute("UPDATE page SET [email protected] WHERE [email protected]",
null, DateTime.Now, id) ;
// Get the page, don't worry it's cached so it wont cause a database operation.
var page = Piranha.Models.Page.GetSingle(id) ;

// Invalidate the page from the server cache
  page.InvalidateRecord(page) ;

By calling this method you can easily make sure pages showing your custom entities are kept up to date in the cache. If you find any extensions to this method that you use in your projects, the method is available as a Gist on GitHub.

blog comments powered by Disqus