Cache Yahoo! Web Service REST Calls with PHP

Caching is essential to decrease network loads and improve overall reliability of your Web applications. In this HOWTO, we demonstrate simple caching techniques that are both fast and reliable.

Caching Requests With a File Swap Cache

A file swap cache is a simple flat-file caching technique. In essence, it does nothing more than store your Web service requests in local files. When a file becomes too old (when the cache becomes "stale"), the file is deleted and replaced by a fresh Web service request.

To make a Web services request using the cache, you call the caching function instead of making a direct request.

$response = request_cache($request, $cache_fullpath, $cache_timeout); 

The three parameters are the actual request URL, the path to the cached version of the request, and the number of seconds before the cache becomes stale:

$request =  'http://search.yahooapis.com/ImageSearchService/V1/imageSearch?appid=YahooDemo&query=Madonna&results=1'; 
$cache_fullpath = '/mydir/ImageMad1'; 
$cache_timeout = 7200;

Here, we arbitrarily chose the name ImageMad1 as the cache file name corresponding to this particular Web services request. Typically, however, you would have a unique file name corresponding to each variation of Web services request. So, you would need to come up with a mapping function to map requests into file names if you want to use the caching with different variations of Web services requests.

The function request_cache does all the real work. First, it checks to see if the request has already been cached or if it has become stale.

function request_cache($url, $dest_file, $timeout) { 
   if (!file_exists($dest_file) || filemtime($dest_file) < (time()-$timeout)) { 

If the request has not been cached or it is stale, then we make the request and cache it:

 
$data = file_get_contents($url); 
$tmpf = tempnam('/tmp','YWS'); 
$fp = fopen($tmpf,"w"); 
fwrite($fp, $data); 
fclose($fp); 
rename($tmpf, $dest_file); 
return $data; 

Otherwise, we return the contents of the cached request:

 
} else { 
   return file_get_contents($dest_file);

A potential problem arises if more than one user of your Web application accesses a cached file just as it is being deleted and refreshed. The statement rename($tmpf, $dest_file) appears to destroy the currently cached version of the file (if any). However, the way the Unix filesystem is implemented guarantees that any process currently reading the previous cache file will continue to read it until it closes the file. So, no process will be cut off or otherwise damaged by the rename.

A complete code sample is available here.

Caching Requests With APC

The Alternative PHP Cache (APC) is a fast caching mechanism available as a PHP extension. Though APC was originally devised as a cache for optimizing PHP intermediate code, it can be used quite handily for everyday caching tasks. The extension must be installed and enabled before it can be used. Downloads and documenation information are available here. To determine if it's installed and enabled, you can run the phpinfo function and browse the output:

<?php
   echo phpinfo();
?> 

To make a Web services request using the cache, you call the caching function instead of making a direct request.

$response = request_cache($request,  $cache_timeout); 

The two parameters are the actual request URL and the number of seconds before the cache becomes stale:

$request =  'http://search.yahooapis.com/ImageSearchService/V1/imageSearch?appid=YahooDemo&query=Madonna&results=1'; 
    $cache_timeout = 7200;

The URL is used as the key to retrieve the cached request which makes APC a very simple cache for developers since you do not need to map Web service requests into unique keys.

function request_cache($url, $ttl) { 
	if(!$xml = apc_fetch($url)) { 
	    $xml = file_get_contents($url); 
	    if ($xml === false) return false; 
	    apc_store($url,$xml, $ttl); 
	 } 
	return $xml; 
	}
 

A complete code sample is available here.

For More Information

For more information on using PHP with Yahoo! Web Services APIs, see The Yahoo! Developer Network PHP Developer Center.