I have a personal project to cache some rss feeds locally on my NAS drive (using it's built-in web server and a chron job) so that I won't miss "posts" when my desktop machine is switched off.
So far I have a simple php script set up that stores the cache for a single feed in a MySQL database. I will expand this to include multiple feeds and loop through them all, but for now I just want to make sure that what I want to do is possible. As SimplePie clears the cache when it expires, I was thinking of creating a copy of the "cache_data" and "items" tables to use like an archive - if I copy all new records into the new tables then it wouldn't matter if SimplePie clear's it's own cache tables because I already have a copy of the items.
What I need to do now is create the output rss/xml file and I'm wondering if SimplePie can be used for this. I see two possibilities;
I've had a look around the SimplePie documentation and through SimplePie.inc to see if I could find anything to point me in the right direction, but this is my first real php project and SimplePie contains rather a lot of complicated looking code. Any suggestions or pointers would be very appreciated :)
Create your own SimplePie_Cache class, filling in the methods with code that gets and sets from your caching tables, or memcached, filesystem, or wherever. The only thing you need to know is to use $this->name as the name of the cache key for each of the functions.
class MySimplePie_Cache extends SimplePie_Cache {
/**
* Create a new cache object
*
* @param string $location Location string (from SimplePie::$cache_location)
* @param string $name Unique ID for the cache
* @param string $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data
*/
function __construct($location, $name, $extension) {
$this->name = $name;
}
static function create($location, $filename, $extension) {
return new MySimplePie_Cache($location, $filename, $extension);
}
function save($data) {
// TODO: save $data to $this->name cache entry
return true;
}
function load() {
// TODO: load $data from $this->name cache entry
return $data;
}
function mtime() {
return time(); // this will disable any expiration checks
}
function touch() {
return true; // not necessary, we don't need to update times, no expiration
}
function unlink() {
return true; // nothing will be removed from the cache
}
}
Then, register the cache class to your feed:
$feed = new SimplePie();
$feed->set_cache_class("MySimplePie_Cache");