February 2009 Archives

One of the features of Movable Type that give it so much power and flexibility is the templating system. By changing code in the templates, designers and site architects can customize both the content and display of discrete page parts and whole pages, and also change the way the pages are organized into a completed web site. While this feature represents a powerful side of Movable Type, it also presents its own set of problems. In the past, it has been noted that MT took a long time to rebuild.

Recently while working on Advisorbits.com we implemented Template Caching and realized a 4 fold reduction in rebuild times. Here's how the savings works.

When you publish or re-publish a site in Movable Type, pages are regenerated by querying the database. This includes many template modules such as the list of categories in the sidebar as well as the main content for each page. In fact the items on the side bar often have multiple lists with counts for the items in the list, not only necessitating the retrieval of data, but also calculations on the whole database. Fortunately databases are very fast and this only takes a fraction of a second. However each page may require many requests to the database, adding up to a few seconds for each page. In some blogs that can add up to quite a lot of time over the whole rebuild.

As of version 4.0, Movable Type provides a mechanism to tell the templating system only to rebuild certain modules once, and to save those modules, or cache them, for a certain period of time, or until a certain event occurs. (An event such as the creation of a new blog entry.) This can reduce the number of times MT must make requests of the database and considerably speed up the whole rebuild process. In the example mentioned we are running more or less "out-of-the-box" MT blog templates and the rebuild time went from around 4 minutes to just a little more than a minute.

cache.png

Many items are only valid until some data in the blog changes. Template modules and widgets can be cached until an event occurs. Take for instance the list of recent entries found on many blogs. This list only changes when the author creates a new entry. It does not change when a reader makes a comment, and so, even though some pages have to be rebuilt to show the new comment, there is no need to request a list of recent entries from the database and the cached data can be used.

Modules can be cached based on age for a specified number of minutes, hours, or days. You may have a feed from another source on your site. Instead of requesting the data every time a page is built, it may make sense to request that external data only once a day, or every 24 hours. If you have headlines for a more frequently updated news source, you might choose to cache the module only for 15 minutes so you had up-to-date headlines on your blog.

To use caching you must first enable it in the blog publishing preferences. All of these template caching techniques when combined can add up to a large potential time savings when rebuilding your blogs.