fguillot/picoFeed — pull request #54 by Raydiation (2014-10-24 13:24:26+00:00)
Title: Caching

This is a first stab at a simpler Reader class see #50

**Note** Tests not adjusted, waiting for feedback

Feed parsing
============

Download and parse a feed
-------------------------

Try this example from a command line script:

```php
download('http://bbc.co.uk/news');

// in case a feed is found for the url
if ($resource) {
$parser = $reader->getParser($resource->getContent());

$feed = $parser->execute();

if ($feed !== false) {
echo $feed;
}
}
```

- The method `download()` returns `false` when there is something wrong during the download or the feed detection
- The call `$parser->execute()` returns `false` when there is a parsing error

Handle HTTP cache
-----------------

To avoid downloading and parsing the feed each time, it's a good idea to handle the HTTP caching:

1. After the first HTTP request, we save somewhere (in a database) the headers Etag and Last-Modified for the next checks
2. If the feed is not modified, we don't need to parse again the feed

Example:

```php
use PicoFeed\Reader;

$reader = new Reader;

// Get last modified infos from previous requests
$lastModified = '...';
$etag = '...';

// Download directly the feed
$resource = $reader->download('http://linuxfr.org/news.atom', $lastModified, $etag);

// Return true is the feed has changed
if ($resource && $resource->isModified()) {
$parser = $reader->getParser($resource->getContent());

$feed = $parser->execute();

if ($feed !== false) {

// Save cache infos for the next request
$lastModified = $resource->getLastModified();
$etag = $resource->getEtag();
$feed_url = $resource->getUrl();
}
}
```