-- Peter Bowyer <[email protected]> wrote
(on Thursday, 09 February 2012, 02:03 AM -0800):
> I'm using Zend_Cache to cache the results of remote API calls. When the
> cache expires we attempt to update the cached data, but sometimes the API
> times out or fails for other reasons.
>
> In this situation I want to continue to use the (now-expired) cached data
> until we can update the cache, but cannot get this to work with Zend_Cache,
> even using the $doNotTestCacheValidity parameter.
>
> My test code is available at:
> http://stackoverflow.com/questions/9002445/zend-cache-retrieve-expired-data
>
> Is this possible with Zend_Cache? I know my architecture is not ideal, since
> every hit to the expired cache (in the vent of the API being down) is going
> to try to update the cache, so an option to increase the cache lifetime
> would be more performant.
>
> The only way I can see at present is to change my caching strategy entirely,
> having a cron-run script to update each cache, and an infinite lifetime for
> every file. I really don't want to centralise everything in this way if
> avoidable.
Your situation is a hard one, because you're not in control of when the
data changes. Normally, I'd recommend a cache invalidation strategy,
where you use infinite lifetimes, and invalidate the cache when data is
saved or updated. However, since that happens in the remote API, it
makes this strategy harder to achieve.
One way around it is to cache the timestamp of when you last performed
the same API call. The workflow would look like this:
* Do I have a timestamp for this API call?
* If not, make the request, and cache the data afterwards.
* If so, is it within a range where I can pull from the cache?
* If it is, pull from the cache.
* If not, attempt to make the API call. If it fails, pull from
the cache anyways. Otherwise, replace the cache.
In this scenario, you _still_ do an infinite lifetime strategy; the
difference is simply that you're caching two separate values --
timestamp of last API call, and the data from that call.
--
Matthew Weier O'Phinney
Project Lead | [email protected]
Zend Framework | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc
--
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]