gpt4 book ai didi

php - SimplePie 缓存功能(可能对其进行扩展)

转载 作者:可可西里 更新时间:2023-11-01 08:52:49 25 4
gpt4 key购买 nike

我有一个个人项目,可以在我的 NAS 驱动器上本地缓存一些 rss 提要(使用它的内置网络服务器和一个 chron 作业),这样当我的台式机关闭时我就不会错过“帖子”。

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

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

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

我已经查看了 SimplePie 文档并通过 SimplePie.inc 看看是否能找到任何东西指向正确的方向,但这是我的第一个真正的 php 项目,SimplePie 包含相当多的复杂外观代码。任何建议或指示将不胜感激:)

最佳答案

创建您自己的 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 = new SimplePie();
$feed->set_cache_class("MySimplePie_Cache");

关于php - SimplePie 缓存功能(可能对其进行扩展),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10323902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com