Caching reduces the number of REST requests needed by temporarily storing data that can be quickly retrieved. Although you do not need to cache the data returned from Web services, doing so, decreases network loads and improves the efficiency of your Web applications. This tutorial will implement a file swap cache and then introduce the PHP accelerator called Accelerated PHP Cache (APC) in the Exploring section.
A file swap cache is a simple flat-file caching technique that stores 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.
We are going to create a function called
request_cache to handle caching. Our function will
need to be able to make a request as well as read and write to a file.
In order to perform these functions, three pieces of information will be
passed as arguments to request_cache: the URI for the request, the name
of the cache file, and the maximum lifetime of the cache file. The
return value of
request_cache will be a string; the
parsing of the response will be done later, thus allowing the data to be
in different formats (JSON, serialized PHP, XML).
When using a cache for any application, the first task is to
determine whether there is an existent cache or if it is "stale"
(expired). If a "fresh" cache exists,
simply returns the data stored in the cache file. Otherwise,
request_cache has to make a request, cache the
response, and finally return the response data. Notice that the actual
request is handled by the helper function
make_request, which like the function
curl_exec returns a response or a Boolean value of
false if the request fails.
rename($tmpf, $dest_file)prevents the previous cache file from being deleted until other processes finish reading and closing the file. So, no process will be cutoff or otherwise damaged by the rename.
make_requestreuses code we wrote earlier in the tutorial. The same code to make the request and remove the HTTP header from the response is repeated, but the task of checking the HTTP status code is delegated to the function
status_code_check. In effect, we have modularized the tasks of making the request and checking the HTTP status codes with separate functions.
make_request handles the job of
checking HTTP status codes to the function
status_code_checkreturns a Boolean value or exits and displays an error message.
request_cache. The return value for
request_cacheis then assigned to the variable
$data, which is later parsed.
The Alternative PHP Cache (APC) is part of the PHP Extension
Community Library (PECL). The APC works by caching compiled bytecode of
PHP programs, thus, reducing the need for the PHP Zend engine to parse
and compile source code for each request. The extension must be
installed and enabled before it can be used. To determine if it's
installed and enabled, run
php -m and look for apc in
the output. See the HOWTO Cache Yahoo! Web Service REST Calls with PHP for code examples.