As Web applications become more and more complex, cleverdevelopers can use application-level caching to improve the performanceof their PHP scripts. This article shows you how, discussing the PEARCache_Lite class with examples that illustrate how it can be used in alive environment.
Now that you know the theory, let's wrap this tutorial up with a real-world example of how useful the Cache_Lite class can be in improving performance on your Web site. As you may (or may not) know, Amazon.com recently opened up their product catalog, allowing developers to create Amazon-backed online stores with their new AWS service. This service allows develoeprs to interact with Amazon.com's catalog and transaction system using SOAP, and it represents an important milestone in the progress of XML-based Web services.
The only problem? Querying the AWS system, retrieving XML-encoded data and formatting it for use on a Web page is a complex process, one which can affect the performance of your store substantially. In order to compensate for this, clever developers can consider caching some of the AWS data on their local systems, reducing the need to query Amazon.com for every single request and thereby improving performance on their site.
Here's the script,
// include Cache_Lite_Output class
// set an ID for this cache
$id = "MyStore";
// configure the cache
$options = array(
"cacheDir" => "cache/",
"lifeTime" => 1800
// instantiate the cache
$objCache = new Cache_Lite_Output($options);
// does data exist in cache?
// if so, print it
// else regenerate it
// include SOAP class
// create a instance of the SOAP client object
// uncomment the next line to see debug messages
// $soapclient->debug_flag = 1;
// create a proxy so that WSDL methods can be accessed directly
$proxy = $soapclient->getProxy();
// set up an array containing input parameters to be
<td align="left" valign="top">
Price: <? echo $i['ListPrice']; ?>
/Amazon.com Price: <? echo $i['OurPrice'];
valign="top" ><font size="1"> Release Date: <?
echo $i['ReleaseDate']; ?></font></td>
<td align="left" valign="top">
href="<? echo $i['Url']; ?>">Read more about this title on
<tr> <td colspan=2> </td>
Disclaimer: All product data on this page belongs
to Amazon.com. No guarantees
are made as to accuracy
of prices and information. YMMV!
and here's what the output looks like:
I'm assuming here that you already know how to use AWS (in case you don't, flip the page for some links to tutorials that will teach you the basics, and remember that you'll need a free AWS developer token for the script above to work) and instead will focus on the caching aspects of the system.
Since this Web page contains intermingled HTML and PHP code, it's convenient to use the Cache_Lite_Output class here. The first task, obviously, is to initialize and configure the cache; I've set the cache to refresh itself at 30-minute intervals, since this seems like a reasonable period of time. Next, I've used the start() method to see if any data already exists in the cache, and display it if so.
If no data exists, the NuSOAP PHP class is include()-d in the script, a SOAP client is instantiated, and a request is made to Amazon.com to obtain a list of bestsellers (node ID 100 in the AWS system). The response is then parsed and formatted into a Web page suitable for display; it is also simultaneously saved to the cache so that future requests for the same page can be served instantly, without having to query the AWS system each time. The end result: faster responses to user clicks, and an overall enhancement in user perception of your site's performance.