PHP
  Home arrow PHP arrow Page 8 - Caching With PHP Cache_Lite
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Caching With PHP Cache_Lite
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 18
    2003-06-06

    Table of Contents:
  • Caching With PHP Cache_Lite
  • The Food Chain
  • Return Of The Jedi
  • Digging Deeper
  • In And Out
  • Bits And Bytes
  • No News Is Good News
  • Cache Cow
  • Endgame

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Caching With PHP Cache_Lite - Cache Cow


    (Page 8 of 9 )

    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


     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway