Home arrow PHP arrow Page 8 - Caching With PHP Cache_Lite

Cache Cow - PHP

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.

TABLE OF CONTENTS:
  1. Caching With PHP Cache_Lite
  2. The Food Chain
  3. Return Of The Jedi
  4. Digging Deeper
  5. In And Out
  6. Bits And Bytes
  7. No News Is Good News
  8. Cache Cow
  9. Endgame
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 22
June 06, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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,




<?php


// include Cache_Lite_Output class

require_once("Output.php");


// 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

if (!$objCache->start($id))

{


// include SOAP class

include("nusoap.php");


// create a instance of the SOAP client object

$soapclient =
new soapclient
("http://soap.amazon.com/schemas2/AmazonWebServices.wsdl",
true);



// 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

// passed to the remote procedure

$params = array(

'browse_node' => 1000,

'page' => 1,

'mode' => 'books',

'tag' => 'melonfire-20',

'type' => 'lite',

'devtag' => 'YOUR_TOKEN_HERE',

'sort' => '+salesrank'

);



// invoke the method

$result = $proxy->BrowseNodeSearchRequest($params);


// check for errors

if ($result['faultstring'])

{

echo $result['faultstring'];

}

else

{

// no errors?

$total = $result['TotalResults'];

$items = $result['Details'];

// format and display the results

?>


?>

<html>

<head>

<basefont face="Verdana">

</head>

<body bgcolor="white">

<p>&nbsp;<p>

<table width="100%" cellspacing="0" cellpadding="5">

<tr>

<td bgcolor="Navy">
<font color="white" size="-1">
<b>Bestsellers</b>
</font></td>

<td bgcolor="Navy" align="right">
<font color="white"
size="-1"><b><? echo date("d M Y",mktime());?></b>
</font></td>
</tr>
</table> <p>
Browse the catalog below:
<p> <table
width="100%" border="0" cellspacing="5" cellpadding="0">


<?php

// parse the $items[] array and
// extract the necessary information

// (image, price, title, catalog, item URL)

foreach ($items as $i)

{

?>

<tr>

<td align="center" valign="top" rowspan="4">
<a
href="<? echo $i['Url']; ?>">
<img border="0"
src=<? echo $i['ImageUrlSmall']; ?>>
</a>
</td> <td>
<font
size="-1"><b><? echo $i['ProductName']; ?>
</b>

/ <? echo $i['Catalog']; ?> / <? echo $i['Manufacturer']; ?>
</font>
</td>

</tr> <tr>

<td align="left" valign="top">
<font size="-1">
List
Price: <? echo $i['ListPrice']; ?>
/Amazon.com Price: <? echo $i['OurPrice'];
?>
</font></td>
</tr> <tr>
<td align="left"
valign="top" ><font size="1"> Release Date: <?
echo $i['ReleaseDate']; ?></font></td>
</tr>
<tr>
<td align="left" valign="top">
<font size="-1">
<a
href="<? echo $i['Url']; ?>">Read more about this title on
Amazon.com</a>
</font>
</td>
</tr>
<tr> <td colspan=2>&nbsp;</td>
</tr>

<?

}

?>

</table>

<font size="2">

Disclaimer: All product data on this page belongs
to Amazon.com. No guarantees

are made as to accuracy
of prices and information. YMMV!
</font>
</body>

</html>


<?php

}

$objCache->end();

}

?>

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.



 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: