Using XML_RPC2 with PEAR - Cached Results
(Page 4 of 4 )
Another great feature of the XML_RPC2 library is the ability to cache results. If your site makes heavy use of a remote API, and the results don't change very often, this can be a great performance gain.
To enable caching, with all the default caching options, we simply change the first two lines of our client example to include CachedClient and instantiate a CachedClient instance instead of a regular client instance:
require('XML/RPC2/CachedClient.php');
$client = XML_RPC2_CachedClient::create(
'http://localhost/xml_rpc_server.php',
array('debug' => true, 'prefix' => 'time.'));
If you reload the page a few times, you'll see that the server transactions only occur on the first call. Caching on the server side is equally simple, substituting CachedServer for Server.
Additionally, you can pass some options to control caching on the client or server end:
$options = array(
'cacheOptions' =>
array(
'cacheDir' => '/tmp', // Where to store cache files
'lifetime' => 3600, // How long to store cache
// Whether to require a parameter to enable the cache
'cacheByDefault' => true,
)
)
In Conclusion
While XML-RPC is not the only option for talking across the web to other services, it is an excellent one. It is light-weight, simple to consume from many languages and platforms, and has been proven effective in many situations. XML_RPC2 in PEAR makes it an even more viable solution for your PHP website, abstracting away all the details and presenting a simple, easy to understand interface. The next time you need to make or consume a web service, keep XML_RPC2 in mind!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |