| <home / programming / phpanth3 / 1 | [previous] [next] |
|
|
Cache_Lite’s in-built lifetime mechanism for cache files provides a good foundation for keeping your cache files up to date, but there will be some circumstances in which you need the files to be updated immediately. For such cases, the methods remove and clean come in handy. The remove method is designed to delete a specific cache file; it takes the cache ID and group name of the file. To delete the page body cache file we created above, we’d use:
$cache->remove('body', 'Dynamic');
Using the clean method, we can delete all the files in our cache directory simply by calling the method with no arguments; alternatively, we can specify a group of cache files to delete. If we wanted to delete both the header and footer created above, we could do so like this:
$cache->clean('Static');
The remove and clean methods should obviously be called in response to events within an application. For example, if you have a discussion forum application, you probably want to remove the relevant cache files when a visitor posts a new message. Although it may seem like this solution entails a lot of code modifications, with some care it can be applied to your application in a global manner. If you have a central script that’s included in every page a visitor views, you can simply watch for incoming events (e.g. a variable like $_GET['newPost']) and have some code respond by deleting the required cache files. This keeps the cache file removal mechanism central and easier to maintain. You might also consider using the php.ini setting auto_prepend_file to include this code in every PHP script.
In Chapter 2, XML , we looked at accessing remote Web services with SOAP and XML-RPC. Because Web services are accessed over a network, it’s often a very good idea to cache results so that they can be fetched locally, rather than repeating the same slow request multiple times. A simple approach might be to use PHP sessions, as we considered in that chapter, but as this solution operates on a per visitor basis, the opening requests for each visitor will still be slow. This is where Cache_Lite can come in very handy.
The PEAR Web installer (see Appendix D, Working with PEAR ) takes advantage of Cache_Lite by caching the XML-RPC requests it makes to the PEAR Web server.
In the section called “How do I consume SOAP Web services with PHP?”, we built a client for a SOAP Web service based on its WSDL file; the service provided weather information for airports around the world. Here’s the code that fetched the data from the remote server:
$countries = $stationInfo->listCountries();
and
$country = $stationInfo->searchByCountry($_GET['country']);
In both cases, these calls correspond to a request for data that’s made over the network. Using Cache_Lite_Function, we could cache the results so the data returned from the service could be reused; this would avoid unnecessary network calls and significantly improve performance. Note that we’re focusing on only the relevant code here. At the top, we include Cache_Lite_Function:
Example 5.11. 5.php (excerpt)
// Include PEAR::Cache_Lite_Function require_once 'Cache/Lite/Function.php';
Further down, we instantiate the Cache_Lite_Function class with some options:
Example 5.12. 5.php (excerpt)
// Define options for Cache_Lite_Function // NOTE: fileNameProtection = TRUE! $options = array( 'cacheDir' => './cache/', 'fileNameProtection' => TRUE, 'writeControl' => TRUE, 'readControl' => TRUE, 'readControlType' => 'strlen', 'defaultGroup' => 'SOAP' ); // Instantiate Cache_Lite_Function $cache = new Cache_Lite_Function($options);
It’s important that the fileNameProtection option is set to TRUE (this is in fact the default value, but in this case I’ve set it manually to emphasize the point). If it were set to FALSE, the filename will be invalid, so the data will not be cached.
Here’s how we make the calls to our SOAP client class:
Example 5.13. 5.php (excerpt)
$countries = $cache->call('stationInfo->listCountries');
And:
Example 5.14. 5.php (excerpt)
$country = $cache->call('stationInfo->searchByCountry',
$_GET['country']);
If the request is being made for the first time, Cache_Lite_Function stores the results as serialized arrays in cache files (not that you need to worry about this), and this file is used for future requests until it expires. The setLifeTime method can again be used to specify how long the cache files should survive before they’re refreshed; right now, the default value of 3,600 seconds (one hour) is being used.
In general Cache_Lite provides a solid, easy-to-implement library for solving caching issues. As we move to the “next level” of caching, for sites with particularly high traffic, it’s worth examining PEAR::Cache , Cache_Lite’s big brother. PEAR::Cache is a complete caching framework that offers greater flexibility than Cache_Lite, and ties in with database abstraction libraries such as PEAR::DB . It also offers advanced features such as caching to shared memory, as an alternative to the file system, or, with help from the Msession PHP extension , storing cache data in load balanced sessions, which is particularly useful for load balanced Web servers. Further PEAR::Cache reading material is recommended for at the end of this chapter. Cache_Lite, however, offers more than enough functionality to meet the requirements of the majority of sites.
| home / programming / phpanth3 / 1 | [previous] [next] |
Created: March 27, 2003
Revised: January 2, 2004
URL: http://webreference.com/programming/phpanth3