Probably one of my favorite features of the Smarty engine is its cache ability. I have little experience with any other cache systems so I’m not going to do any sort of comparison, but I absolutely love the way Smarty handles caches.
We are using Smarty on FugitiveThought here, which is basically our biggest line of defense against any Digg /Slashdot effects whenever a link gets posted. A few things to note about our site: 1) the front page is dynamic, but if you’ve been here often you probably notice that it doesn’t get updated very often. 2) The comments page (where you can read the blog post and view the comments written below) is slightly more dynamic because we get more comments than blog posts (usually!). This would be more true for something like Slashdot or a larger discussion site.
Caching the Front Page
Okay, so we want a static HTML cache for the front page so that whenever a viewer requests the page, the only thing that has to be done in PHP is to check if the cache exists, and then dump it to the browser. Inside our index.php file, we just need to have the code: $smarty->caching = 2;
This turns caching on. The value of 2 indicates that the cache lifetime that we set in the code is unique to this page rather than setting a global.
The code to check if the cache exists is: if(!$smarty->is_cached('frontpage.tpl')) { /* Generate */ }
Basically all this does is check if our front page has already been rendered (aka, there is a valid, unexpired cache already saved for it). If this doesn’t exist, then it does the regular stuff like grabbing the blog entries, counting the comments, etc. and then at the very end of our index.php we have: $smarty->display('frontpage.tpl');
Which displays the template.
That’s it! If the template was already cached, then the call to display will just dump the existing cache. Otherwise it will update it with all of the new values and then display it, saving it to the cache.
The only real issue we came across was the fact that the top-right navigation menu that we use for logging in for account and blog management work can’t be cached. If it is then we won’t have our handy links when we log in and the site wouldn’t retain its (at least mildly) contiguous feel. Thankfully Smarty has this all figured out for us too. In the area where we want to display dynamic content, we put the code: {insert name="getLoginBanner"}
into our template file. The insert tag calls a function named getLoginBanner in PHP that outputs the contents that you would like to display. This function will always be called even from cached files, so try to minimize its processor time, otherwise the entire point of caching is… well, pointless!
Caching The Comments
For the front page, we can get away with putting a 30 minute timeout on the cache (which makes a massive difference in processor usage if you’re getting 15,000 hits per day from a digg-dotting). But we would like more responsiveness on comments page. We can base our cache-regenerating decision on the fact that the majority of the web users are leeches. That is, there will always be more people reading the page than contributing to it. This is even true if you count all of the spam-bots that we have to fight off! Based on this, we decided to just regenerate the comments page cache for each time a user submits a new comment. So in the comments page, after the new comment is inserted into the database, we run: $smarty->clear_cache("comments.tpl", $index);
You see an interesting thing here. Not only is this clearing the cache for “comments.tpl” (the template that describes how to display the comments page), but it is including the unique $index of this particular blog post. Since we use a single template file to describe so many different pages, we want a unique cache for each page. For the comments page, the cache was created with: if(!$smarty->is_cached('comments.tpl',$cache_id))
The extra parameter describes a unique version of the template. Whenever a comment is added to the page for one blog entry, only the cache for that entry is regenerated.
Conclusions
Okay, that was a really fast crash course, so if you want more information, I recommend looking at the Smarty documentation. In summary, this will very much help to reduce strain on your server and it is not complicated at all to use especially if you are already using Smarty. I am going to be testing out the PECL PHP Cache this summer one a different site and I’ll report back on how easy it is to use that!