13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Solution Overview<br />

847<br />

}<br />

if($type == 'browse') {<br />

$ars->browseNodeSearch($parameters['browsenode'], $parameters['page'],<br />

$parameters['mode']);<br />

}<br />

if($type == 'search') {<br />

$ars->keywordSearch($parameters['search'], $parameters['page'],<br />

$parameters['mode']);<br />

}<br />

cache($type, $parameters, $ars);<br />

}<br />

return $ars;<br />

All the application’s SOAP or XML caching is done via this function.You also use<br />

another function to cache images.You begin by calling the cached() function to see<br />

whether the required AmazonResultSet is already cached. If it is, you return that data<br />

instead of making a new request to Amazon:<br />

$cache = cached($type, $parameters);<br />

if($cache) // if found in cache{<br />

return $cache;<br />

}<br />

If not, when you get the data back from Amazon, you add it to the cache:<br />

cache($type, $parameters, $ars);<br />

Let’s look more closely at these two functions: cached() <strong>and</strong> cache().These functions,<br />

shown in Listing 33.12, implement the caching Amazon requires as part of its terms <strong>and</strong><br />

conditions.<br />

Listing 33.12 cached() <strong>and</strong> cache() Functions—Caching Functions from<br />

cachefunctions.php<br />

// check if Amazon data is in the cache<br />

// if it is, return it<br />

// if not, return false<br />

function cached($type, $parameters) {<br />

if($type == 'browse') {<br />

$filename = CACHE.'/browse.'.$parameters['browsenode'].'.'<br />

.$parameters['page'].'.'.$parameters['mode'].'.dat';<br />

}<br />

if($type == 'search') {<br />

$filename = CACHE.'/search.'.$parameters['search'].'.'<br />

.$parameters['page'].'.'.$parameters['mode'].'.dat';<br />

}<br />

if($type == 'asin') {<br />

$filename = CACHE.'/asin.'.$parameters['asin'].'.'<br />

.$parameters['mode'].'.dat';

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!