Quick Caching With PHP Sessions
Published by Mike July 29th, 2006 in Code, PHPIn developing mashups, we’re often in a position to be frequently making calls to outside data sources. A finished application might query something once, parse it, and then save the results, but sometimes that parsing can take more than one try to get correct.
Why spend time setting up a sophisticated cache for the whole responses while you develop your XPath expressions and regexes? Especially if you ultimately will only be caching the results of these actions?
What’s needed is a quickie caching system, one that eschews the complications of file-access and DB tables, but still prevents every little change from hammering away on someone else’s server.
One solution is to simply store the responses in the PHP variable $_SESSION, and then read them out again on the next viewing. To make it even easier, you can just wrap your remote calls in some code that checks it for you.
Wrapping CURL
As useful as it is, I get annoyed by the amount of configuration that CURL requires. As such, I was already using a wrapper class—this one, in fact, by Sean Huber.
The final product was complicated slightly by my desire to preserve Sean’s callback functionality, but the basis of the system is a few simple modifications to the doRequest method:
- Create a key for the cache, by serializing the function’s arguments, and then hashing the result.
- Check if that key exists in the array
$_SESSION['curl_cache']. If so, simply return the cached response. - Afterward, grab the whole response as received, and cache it for the next pageview.
You can see the full implementation here: curl.php with caching.
To make use of it, you just initialize the class,
require('../curl.php');
$curl = new CURL();
$curl->enableCache();
And then make your requests:
$curl->get($the_url)
If a cached response exists, that’s what you’ll get. Otherwise, it’ll fetch it from the remote source.
Notes
This is not a “production ready” caching mechanism. It doesn’t allow data to be shared among users, it depends on browser cookies, and it doesn’t have any clear concept of cache lifetimes (although you can call CURL::flushCache to dump all the data en masse).
Nonetheless, it is a simple and flexible method to use in development cycles. An upcoming map project I’m working involves mashing up data from Wikipedia—it would be dreadful to cause any unnecessary traffic to WP by spidering their pages any more than necessary.
Don’t forget the power of the PHP Session, especially if all you need is a quick “memory” between pageviews.



