Resources for Developers Using Amazon Associates Web Services
· Home  
· Search  
· Browse Nodes  
· Data Feed?  
· One-Second Rule  
· PHP Examples  
· Tools  
· Understanding A2S  

Create a custom Amazon Associate Store in minutes with Associate-O-Matic.

Getting REST Results with Simple Caching

Here is a function with demonstrates how to combine fetching REST results from Amazon's A2S servers with a simple caching scheme using a MySQL database.

function getPage($url, $maxage=7, $cache = true)
 {
  $body = "";
  if ($cache)
    {
     // Get cached result
     $result = mysql_query("SELECT Body, TO_DAYS(NOW()) "
       . "- TO_DAYS(Updated) FROM amazoncache WHERE "
       . "URL = '" . addslashes($url) . "'");
     if (list($body, $age) = mysql_fetch_row($result))
       {
        // If not too old, return cached value
        if ($age <= $maxage)
          {
           return $body;
          }
       }
    }

  $ch = curl_init(); 
  curl_setopt ($ch, CURLOPT_URL, $url); 
  curl_setopt ($ch, CURLOPT_USERAGENT, 
    'YourName/1.0 (+http://www.yoursite.com/)'); 
  curl_setopt ($ch, CURLOPT_HEADER, 1); 
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
  curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt ($ch, CURLOPT_TIMEOUT, 120);

  $result = curl_exec ($ch);

  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  $hsize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  $result = substr($result, $hsize);

  // On error, try using old cached result
  if ($code >= 400)
    {
     $code = $body;
    }

  curl_close($ch);

  // Store returned value in cache
  $xbody = trim(addslashes($result));
  mysql_query("INSERT IGNORE INTO amazoncache (URL, Body, "
    . "Updated) VALUES ('$url', '$xbody', NOW()) "
    . "ON DUPLICATE KEY UPDATE Body='$xbody', "
    . "Updated=NOW()");
  return $result; 
 }

The function accepts three arguments. The first is the URL. The second is the maximum age (in days) results should be cached. The third allows you to bypass the cache by setting it to false. Only the first argument is required, the others are optional. The function returns the results of the query.

The method assumes that the current database contains a table called amazoncache with three fields. You can create the table using the following SQL statement:

CREATE TABLE `amazoncache` (
  `Cache_id` int(10) NOT NULL auto_increment,
  `URL` text NOT NULL,
  `updated` datetime default NULL,
  `body` text,
  PRIMARY KEY  (`Cache_id`),
  UNIQUE KEY `URL` (`URL`(255)),
  KEY `Updated` (`updated`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

The table has a Unique index on the URL which is used as the key for the cached data. An index on the Updated column makes periodic purging of the database quicker. You should periodically (i.e. once a day) purge the amazoncache table with a query like:

DELETE FROM amazoncache WHERE TO_DAYS(NOW()) - 
  TO_DAYS(Updated) > 90

Copyright © 2008 by Roger Smith