PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Cache Support and Intelligent Requests

Adding Cache Support to Zend_Feed_Reader

Zend_Feed_Reader supports using an instance of Zend_Cache to cache feeds (as XML) to avoid unnecessary network requests. Adding a cache is as simple here as it is for other Zend Framework components, create and configure your cache and then tell Zend_Feed_Reader to use it! The cache key used is "Zend_Feed_Reader_" followed by the MD5 hash of the feed's URI.

<?php
$frontendOptions 
= array(
   
'lifetime' => 7200,
   
'automatic_serialization' => true
);
$backendOptions = array('cache_dir' => './tmp/');
$cache Zend_Cache::factory(
    
'Core''File'$frontendOptions$backendOptions
);

Zend_Feed_Reader::setCache($cache);

Note

While it's a little off track, you should also consider adding a cache to Zend_Loader_PluginLoader which is used by Zend_Feed_Reader to load Extensions.

HTTP Conditional GET Support

The big question often asked when importing a feed frequently, is if it has even changed. With a cache enabled, you can add HTTP Conditional GET support to your arsenal to answer that question.

Using this method, you can request feeds from URIs and include their last known ETag and Last-Modified response header values with the request (using the If-None-Match and If-Modified-Since headers). If the feed on the server remains unchanged, you should receive a 304 response which tells Zend_Feed_Reader to use the cached version. If a full feed is sent in a response with a status code of 200, this means the feed has changed and Zend_Feed_Reader will parse the new version and save it to the cache. It will also cache the new ETag and Last-Modified header values for future use.

These "conditional" requests are not guaranteed to be supported by the server you request a URI of, but can be attempted regardless. Most common feed sources like blogs should however have this supported. To enable conditional requests, you will need to provide a cache to Zend_Feed_Reader.

<?php
$frontendOptions 
= array(
   
'lifetime' => 86400,
   
'automatic_serialization' => true
);
$backendOptions = array('cache_dir' => './tmp/');
$cache Zend_Cache::factory(
    
'Core''File'$frontendOptions$backendOptions
);

Zend_Feed_Reader::setCache($cache);
Zend_Feed_Reader::useHttpConditionalGet();

$feed Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');

In the example above, with HTTP Conditional GET requests enabled, the response header values for ETag and Last-Modified will be cached along with the feed. For the next 24hrs (the cache lifetime), feeds will only be updated on the cache if a non-304 response is received containing a valid RSS or Atom XML document.

If you intend on managing request headers from outside Zend_Feed_Reader, you can set the relevant If-None-Matches and If-Modified-Since request headers via the URI import method.

<?php
$lastEtagReceived 
'5e6cefe7df5a7e95c8b1ba1a2ccaff3d';
$lastModifiedDateReceived 'Wed, 08 Jul 2009 13:37:22 GMT';
$feed Zend_Feed_Reader::import(
    
$uri$lastEtagReceived$lastModifiedDateReceived
);

Zend Framework