2012-04-25 45 views
3

我有一个个人项目来缓存我的NAS驱动器上的一些rss提要(使用它的内置web服务器和chron作业),这样我就不会错过“posts”我的台式机被关闭。SimplePie缓存功能(可能扩展它)

到目前为止,我有一个简单的PHP脚本设置,它将单个feed的缓存存储在MySQL数据库中。我将扩展它以包含多个提要并遍历它们,但现在我只想确保我想要做的事情是可能的。当SimplePie在缓存过期时清除缓存时,我正在考虑创建一个“cache_data”和“items”表的副本,以便像存档一样使用 - 如果我将所有新记录复制到新表中,那么SimplePie无关紧要明确的是它自己的缓存表,因为我已经有一个项目的副本。

我现在需要做的是创建输出rss/xml文件,我想知道是否可以使用SimplePie。我看到两种可能性;

  1. Get SimplePie使用“存档”表,因为它的缓存位置已禁用,因此不会删除任何内容。
  2. 我自己从“arcive”表中读取数据,并使用SimplePie处理数据并构建rss提要。

我已经有文件了SimplePie围绕和通过SimplePie.inc看看,看看我能找到任何点我在正确的方向,但是这是我第一次真正的PHP项目,包含了SimplePie而很多复杂的代码。任何建议或指针将非常感谢:)

+0

为什么不杀青了SimplePie上缓存的时间? – Brad 2012-04-25 21:16:22

+0

rss提要每隔几分钟就会发布新帖子,并将项目数限制为300.我需要每隔几小时更新一次缓存或者可能会错过某些内容。如果我假期休息了一周,那么将缓存时间设置为一周就意味着我可以获得本周初存在的300个帖子,并且会漏掉所有发布的内容,桌面。 – sim099 2012-04-26 07:13:20

回答

1

创建您自己的SimplePie_Cache类,填写方法与代码,从您的缓存表,或memcached,文件系统或任何地方获取和设置。您唯一需要知道的是使用$ this->名称作为每个函数的缓存键的名称。

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 
    } 
} 

然后,注册缓存类供稿:

$feed = new SimplePie(); 
$feed->set_cache_class("MySimplePie_Cache");