Caching for Better Reliability and Performance (Optional)


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.

Getting Started

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, request_cache 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.

  1. The statement 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.
  2. The function make_request reuses 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.

    In turn, make_request handles the job of checking HTTP status codes to the function status_code_check.

  3. The function status_code_check returns a Boolean value or exits and displays an error message.
  4. In the main program, we assign the needed information for caching to variables, which are then passed to request_cache. The return value for request_cache is then assigned to the variable $data, which is later parsed.
  5. Although the process of caching is slightly more complicated than just making requests to a Web service, the efficiency and reliability of caching are significant and necessary when your Web applications are in production. Moreover, by using modularization, we were able to simplify the function request_cache.

Exploring (Optional)

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.

Table of Contents