SimplePie缓存功能(可能扩展它)

时间:2012-04-25 21:14:22

标签: php mysql simplepie

我有个人项目在我的NAS驱动器上本地缓存一些rss源(使用它的内置Web服务器和一个chron作业),这样我就不会错过"帖子&# 34;当我的台式机关闭时。

到目前为止,我已经设置了一个简单的php脚本,用于存储MySQL数据库中单个Feed的缓存。我将扩展它以包含多个feed并循环遍历它们,但是现在我只想确保我想要做的事情是可能的。当SimplePie在缓存过期时清除缓存时,我正在考虑创建一个" cache_data"和"项目"要像归档一样使用的表 - 如果我将所有新记录复制到新表中,那么SimplePie清除它自己的缓存表是不重要的,因为我已经拥有了这些项目的副本。

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

  1. 让SimplePie使用"存档"表,因为它的缓存位置已禁用,因此不会删除任何内容。
  2. 阅读" arcive"表格自己并使用SimplePie处理数据并构建rss feed。
  3. 我已经浏览过SimplePie文档并通过SimplePie.inc查看我是否能找到任何指向正确方向的内容,但这是我的第一个真正的php项目,而SimplePie包含了很多复杂的代码。任何建议或指示将非常感激:)

1 个答案:

答案 0 :(得分:1)

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

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:

$feed = new SimplePie();
$feed->set_cache_class("MySimplePie_Cache");
相关问题