Online
 
Friday, 09 January 2009
 
 
More article:
Related Content:

Optimizing Your PHP Scripts
 
Article Index
Optimizing Your PHP Scripts
Page 2
Page 3
Page 4
Page 5
Page 6
Page 7
Page 8
Page 9
Page 10
Page 11
Page 12
 

In the event the cache does not contain data for the given cache ID (or it is expired), start() will return an empty string and begin an output buffer to capture the generated output. After the output has been generated, the end() method must be called to actually display the generated content and save the content to the cache.

The end() method accepts an optional parameterthe amount of time, in seconds, before the cached version of the content expires. The result to the end user is that the page is displayed as usual; however, from an optimization standpoint the benefits are indisputable.

NOTE

Although it is only necessary that you realize is a relatively slow operation to perform to understand the power of the PEAR Cache, if you are interested in dynamic image generation please refer to , "Working with Images."


Caching Function Calls

Beyond caching entire dynamically generated HTML documents, the PEAR cache also has facilities to cache smaller portions of your PHP scripts, such as the results from particularly expensive function calls. For instance, consider the following function, which uses the GD library to generate an image ():

Listing 10.8. Image Generation Function Example
<?php

define('FONT', 4);
function make_image_word($wordfile, $width, $height) {

$words = array_flip(file($wordfile));
$word = trim(array_rand($words));

$img = imagecreate($width, $height);

$black = imagecolorallocate($img, 0x00, 0x00, 0x00);
$white = imagecolorallocate($img, 0xFF, 0xFF, 0xFF);
imagefill($img, 0, 0, $white);
for($i = 0; $i < 20; $i++) {

$start_x = rand(0, $width);
$start_y = rand(0, $height);
$c_width = rand(0, $width/2);
$c_height = rand(0, $height/2);
$color = imagecolorallocate($img, rand(0x00, 0xFF),
rand(0x00, 0xFF), rand(0x00, 0xFF));

imageellipse($img, $start_x, $start_y,
$c_width, $c_height, $color);

}

return $data;

}

$data = make_image_word("/usr/share/dict/words", 100, 50);
?>

This function is a particularly expensive operation that returns an image with a word in an array suitable for preventing autoregistration by Web bots. To cache the data generated by this function, we'll use the PEAR function cache as shown in (assume the function is still defined):

Listing 10.9. Caching Function Calls Using PEAR
 <?php
require_once('Cache/Function.php');
define('CACHE_EXPIRE', 30);
$cache = new Cache_Function('file',
array('cache_dir' => '.',
'filename_prefix' => 'cache_'),
CACHE_EXPIRE);
$data = $cache->call('make_image_word', '/usr/share/dict/words', 100, 50);
?>

As you can see, caching the results of a function call is a very straightforward process. Unlike the output cache example in , the Cache_Function class requires not only a container and its parameters, but takes a third parameter representing the time in seconds to cache the function result. Furthermore, unlike the Output Cache function, caches do not have any sort of unique cache identifier associated with them. Rather, the function name and its parameters are automatically used. Calling the function using the PEAR function cache is done using the call() method of the Cache_Function class, where the first parameter is the name of the function, and each following parameter represents the parameters to pass to the function being called.

Now that you have an idea of how caching functions works, let's take a look at the performance gains by profiling the standard function call against the cached version:

Method one took 6.4777460098267 seconds.
Method two took 0.78488004207611 seconds.

Method two was faster than Method one by 87.88%

As is quite clear by the profiling statistics, caching provides staggering gains (nearly 90% faster) for high-cost operations.

NOTE

Many professional and open source tools exist that provide excellent tools to both debug and profile your PHP scripts. Zend's PHP development environment (called ZDE) is an excellent commercial product that does both these things and much more. For those of you interested in a more open source approach, the XDebug extension for PHP 5 provides much of the same functionality as the Zend IDEeven if it does so without as nice an interface. See for more information regarding the Xdebug extension for PHP.



Tags: Add more tags...,
This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment. Tags: Simple PHP, Pear, Easy PHP, PHP Tutorial, PHP MySQL, XSLT, Sap Tutorial, CSS Tutorial, XSL FO Java, SQL Tutorial.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Mobile Wallpaper 227
Statistic


Last Post

 
Top! Top!