Chapter 9. Caching and Performance

Caching is a technique that is used very frequently by programmers to avoid calculating or generating the same output repeatedly. Instead, function results or even output are recorded and saved for later reuse. Usually caching is done by saving the appropriate information in special files on disk, and then retrieving it later, but you could just as well cache things in memory.

A simple way to introduce caching to your PHP scripts is by using the static keyword for a variable on functions, and using that variable to keep an in-memory copy of the function results, such as in the following code snippet:

<?php
function getUserID($username)
{
    global $conn;
    static $returns;

    // check if we already have the result of 
    // this function for the given username
    if (!empty($returns[$username])) {
        return $returns[$username];
    }

    $stmt = “SELECT
                user_id
             FROM
                user
             WHERE
                user_name=’” . addslashes($username) . “’”;
    $result = mysql_query($stmt, $conn);
    if ($result) {
        $user_id = mysql_result($result, 0, 0);
        // cache this result for later
        $returns[$username] = $user_id;
        return $user_id;
    }
}

// first call will trigger a DB query
$user_id = getUserID(‘andrei’);
// second call will get its results from the cache
$user_id = getUserID(‘andrei’);
?>

That is a nice example of an in-memory cache system (although extremely simple), but that’s not how Smarty caches templates. Smarty caches the templates files into other files, where the actual output is saved and reused later if caching is enabled. Let’s go over how to use this very handy feature.

Caching in Smarty

By default, a Smarty template has caching disabled (as of version 2.6.10), but you can always enable it manually on your PHP scripts by setting the caching property to 1, as in:

<?php
include_once(‘libs/Smarty.class.php’);
$smarty = new Smarty;
$smarty->caching = 1;

$smarty->display(‘example2.tpl’);
?>

This will speed things up by saving the output of a template file into a cache file, and using that file in the future, instead of regenerating the output again from scratch. In some cases, the speed gain as a result of this feature is dramatic. The lifetime of each cached file defaults to one hour from its creation.

You need to be aware of what this means for your particular application, since this type of caching may not be suitable for everything. That is, refreshing your cached files every hour might be too long for your particular case, or maybe even too short. The lifetime can be tweaked on a per-cached-file basis, by setting the caching property to 2, and then changing the cache_lifetime property as you wish. Here’s an example:

<?php
include_once(‘libs/Smarty.class.php’);
$smarty = new Smarty;

// set the lifetime per cached file
$smarty->caching = 2;

// set the lifetime for 2 hours
$smarty->cache_lifetime = 7200;
$smarty->display(‘example3.tpl’);

// set the lifetime for 1 day
$smarty->cache_lifetime = 24 * 60 * 60;
$smarty->display(‘daily_favorites.tpl’);
?>

In the example above, we set the cache lifetime of the example3.tpl template file to two hours, and set the lifetime to one day for the daily_favorites.tpl template. As you can guess from the name, the template file doesn’t need to be generated that frequently, since it is simply displaying the previous day’s favorite links. As mentioned before, the best way to improve the performance of PHP scripts is to spend some time analyzing your application. See if tweaking the cache lifetime for each particular template will make sense or not.

While these features are extremely useful in some situations, you might want to automatically refresh the cache if a template file was updated. Smarty’s compile_check property allows you to do just that, since it forces the template engine to check whether any template or config files associated with this cache have been updated, and if so, regenerates the cache. In order to enable this feature, just set this property to TRUE, as in:

<?php
include_once(‘smarty.class.php’);
$smarty = new smarty;
$smarty->caching = 1;
$smarty->compile_check = TRUE;

$smarty->display(‘example4.tpl’);
?>

Of course, enabling this feature will affect performance, since Smarty will need to check all the associated files for their modified dates.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.21.86