As for caching, the first thing we do is see if we've got a recent cache of this feed (see feed.php, available online). So that we don't unduly burden service providers, we cache everything from them. Thanks to Zend_Cache, caching is simple.
Not all caches are created equal, though. Because our feed can change frequently, we cache it for 15 minutes. Once we've fetched the keywords for a given article, chances are that Yahoo isn't going to change its mind about them, so we cache them for 24 hours. Flickr, however, may have new pictures uploaded that are more relevant than the ones we currently have, so we cache Flickr responses for two hours.
Caching does two important things for our service:
- It speeds up the application as calls to other services are expensive, time wise.
- By caching the responses for a reasonable time, we protect ourselves from the failures of our partners. Because we are reading from the cache most of the time, if a partner's site becomes unresponsive or unavailable, our application still operates normally. If this were a serious problem, we could turn off the automatic cleaning of the Zend_Cache and only remove old cache entries once it has expired and we know we have a new entry to replace it. This adds a few more lines of code to our Proxy classes, but nothing that is terribly difficult and could make the difference between showing users results and a blank page.
If we do not have a valid cache, the code inside the IF statement triggers and we fetch the current feed. Using the Zend_Feed_RSS class, we instantiate it with the URI of the feed. Since we don't need everything in the feed, we create an array of the relevant pieces of it. We are fetching the feed because we didn't have a valid cache, so before we finish, we save this array in the cache for the next request.
In all the Proxy-based classes for this project, we use an md5() hash as the cache identifier. In the case of the Feed cache, we use an md5() of the URL. For keywords, we hash the content used to generate the keywords; for photos, we use a hash of the keyword we are requesting a photo for. The Zend_Cache class requires that the cache identifier only be alphanumeric. Using md5() is a quick way to ensure that the rule is followed and that we have a unique and retrievable identifier.