Channels ▼
RSS

Web Development

PHP: The Power Behind Web 2.0

Source Code Accompanies This Article. Download It Now.


Caching

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.

Finally, we load the response property of our Proxy class with the array and return so that the service can hand it back to our JavaScript.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 
Dr. Dobb's TV