I'm using Zend_Cache to cache data generated from a web service. However, in the event the web service does not respond, I want to display the outdated information and not leave a blank space.
According to the documentation, the answer is to pass the second argument to Zend_Cache_Core::load()
:
@param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
However, for every test I've done this always returns bool(false)
for expired cache content.
Is there a way to force Zend_Cache to return the cached data for a given cache key, even when it has expired?
$cache_key = md5($url);
if ($out = $cache->load($cache_key)) {
return $out;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
if ($output) {
// Process...
$cn_cache->save($out, $cache_key);
} else {
// The query has timed out/web service not responded
// We need to load the outdated cached content... but the following DOES NOT work
return $cache->load($cache_key, true);
// var_dump($cache->load($cache_key, true)); # false
}
I can't think of a reliable way to do this other than to have a second cached version of the object that never expires. If you set a cache expiration time of X seconds on an object, there's simply no way to guarantee that the object will still be there after X seconds.
Example proposed workaround below...