Caching HTML Output with Smarty

I’ve begun using the Smarty template engine for my projects requiring dynamic content – you know, the good ol’ MVC (model-view-controller) approach to (web) applications. Because the sites I work on aren’t particularly high-traffic, I never really thought too hard about caching… until I actually began thinking about caching. The question is “why not?”.

With servers as quick as they are these days, even highly dynamic pages can be processed rather quickly. On one of my development machines, I’m getting between 65 and 70 fulfilled requests per second on a page with little in the way of optimization and no caching. By adding a simple caching scheme to this page via some built-in Smarty functions, that number jumps to about 105-110 fulfilled requests per second. Super!

Honestly, it’s so simple I might as well just point you to the appropriate documentation that tells you how to do it: HERE. The most important thing to notice is that you at least need $smarty->caching=TRUE;, and for goodness sakes, make sure your cache directory is writable by Apache (I would also make sure to either have your Smarty directory outside the site root or disallow access via a .htaccess file).

Here’s to a 70% performance boost!

Leave a comment

Hey there! Come check out all-new content at my new mistercameron.com!