You are here: Documentation » SimplePie Add-ons » Cache Extras
Table of Contents
Cache Extras
The Basics
Author | Ryan Parman |
---|---|
Revision | 1 |
SimplePie version | 1.0 (not 1.1) |
Classes Extended | SimplePie |
About the Add-on
This add-on adds additional cache-related methods to the SimplePie object such as cache file name, cache timestamp, and cache time remaining.
Installation
Instructions
- Create a new file called
simplepie_cache_extras.inc
and place it in the same directory as yoursimplepie.inc
file. - On the SimplePie-enabled page you want to use this extension on, make sure you include it in the same way that you include
simplepie.inc
.
Add-on Source Code
<?php class SimplePie_Cache_Extras extends SimplePie { function get_cache_object() { $cache =& new $this->cache_class($this->cache_location, call_user_func($this->cache_name_function, $this->feed_url), 'spc'); return $cache; } function get_cache_filename() { $cache = $this->get_cache_object(); return $cache->name; } function get_cache_timestamp() { $cache = $this->get_cache_object(); return $cache->mtime(); } function get_cache_time_remaining($format = false) { $cache = $this->get_cache_object(); $remaining = ($cache->mtime() + $this->cache_duration) - time(); if ($format) { return SimplePie_Misc::time_hms($remaining); } else { return $remaining; } } } ?>
Using the Add-on
Methods
get_cache_object()
Returns the SimplePie_Cache object that SimplePie is using. This is used by the other methods to return their data.get_cache_filename()
Returns the path and filename of the cache file for this feed.get_cache_timestamp()
Returns a UNIX timestamp for the time when the feed was cached.get_cache_time_remaining()
Returns the remaining time (in seconds) until the cache is considered stale, and SimplePie will re-check the feed for freshness. Optionally, you can passtrue
as the parameter, and it will return the remaining time in ahh:mm:ss
format instead.
Example Code
<?php require_once('simplepie.inc'); require_once('simplepie_cache_extras.inc'); // Instead of a new SimplePie(), we'll do a new SimplePie_Cache_Extras() since we extended the SimplePie class. $feed = new SimplePie_Cache_Extras('http://digg.com/rss/index.xml'); $feed->handle_content_type(); // Let's display the filename of the cached feed. echo 'Cached file name: ' . $feed->get_cache_filename() . '<br />'; // When was this cache file created? echo 'File was cached at: ' . $feed->get_cache_timestamp() . ' (' . date('j F Y, g:i a', $feed->get_cache_timestamp()) . ')<br />'; // How long until the cache expires? echo 'Time remaining until next cache check: ' . $feed->get_cache_time_remaining() . ' seconds (' . $feed->get_cache_time_remaining(true) . ' hh:mm:ss)<br />'; echo '<hr />'; foreach ($feed->get_items() as $item) { echo '<a href="' . $item->get_permalink() . '">' . $item->get_title() . '</a><br />'; } ?>
Add-on Source Code (Modified - Sunday 6th June 2010 by Mark Bowen
For anyone who can't get this to work I managed to get it to work by doing the following below. I commented out the main line in the get_cache_object() function and added in the line as shown below which I found in the main simplepie.inc file.
This worked right away and allowed all the cache functions to start working. Hopefully I've not done anything silly here though?
<?php class SimplePie_Cache_Extras extends SimplePie { function get_cache_object() { // $cache =& new $this->cache_class($this->cache_location, call_user_func($this->cache_name_function, $this->feed_url), 'spc'); $cache = call_user_func(array($this->cache_class, 'create'), $this->cache_location, call_user_func($this->cache_name_function, $this->feed_url), 'spc'); return $cache; } function get_cache_filename() { $cache = $this->get_cache_object(); return $cache->name; } function get_cache_timestamp() { $cache = $this->get_cache_object(); return $cache->mtime(); } function get_cache_time_remaining($format = false) { $cache = $this->get_cache_object(); $remaining = ($cache->mtime() + $this->cache_duration) - time(); if ($format) { return SimplePie_Misc::time_hms($remaining); } else { return $remaining; } } } ?>
addons/cache_extras.txt · Last modified: 2011/03/06 03:56 (external edit)