RSSCache
云策文档标注
概述
RSSCache 是 WordPress 中用于缓存 RSS 数据的类,基于 WordPress 的 Transients API 实现。它提供缓存设置、获取、检查和错误处理等功能,支持自定义缓存路径和过期时间。
关键要点
- 类 RSSCache 包含属性如 BASE_CACHE(缓存目录)、MAX_AGE(默认 43200 秒)和 ERROR(错误信息)。
- 构造函数 __construct 和 RSSCache 允许设置缓存目录和过期时间,默认使用 WP_CONTENT_DIR . '/cache'。
- 主要方法包括 set(添加缓存项)、get(获取缓存项)、check_cache(检查缓存状态)、serialize/unserialize(序列化处理)、file_name(URL 映射为 MD5 文件名)、error/debug(错误和调试处理)。
- 缓存操作基于 set_transient 和 get_transient,键名以 'rss_' 前缀和 URL 的 MD5 值组成。
- 错误处理支持 MAGPIE_DEBUG 常量控制,可触发 WordPress 错误或记录到错误日志。
代码示例
// 示例:创建 RSSCache 实例并设置缓存
$cache = new RSSCache('/custom/cache/path', 3600); // 自定义路径和过期时间
$url = 'http://example.com/feed';
$rss_data = 'RSS content';
$cache_option = $cache->set($url, $rss_data); // 设置缓存
$cached_rss = $cache->get($url); // 获取缓存
$status = $cache->check_cache($url); // 检查缓存状态,返回 'HIT' 或 'MISS'注意事项
- 确保缓存目录可写,否则可能影响缓存功能。
- MAX_AGE 设置需根据实际需求调整,避免缓存过期或占用过多资源。
- 使用 Transients API,缓存可能被 WordPress 自动清理,特别是在多站点环境中。
- 错误处理依赖于 MAGPIE_DEBUG 常量,建议在开发环境中启用以方便调试。
原文内容
Methods
| Name | Description |
|---|---|
| RSSCache::__construct | PHP5 constructor. |
| RSSCache::check_cache | – |
| RSSCache::debug | – |
| RSSCache::error | – |
| RSSCache::file_name | – |
| RSSCache::get | – |
| RSSCache::RSSCache | PHP4 constructor. |
| RSSCache::serialize | – |
| RSSCache::set | – |
| RSSCache::unserialize | – |
Source
class RSSCache {
var $BASE_CACHE; // where the cache files are stored
var $MAX_AGE = 43200; // when are files stale, default twelve hours
var $ERROR = ''; // accumulate error messages
/**
* PHP5 constructor.
*/
function __construct( $base = '', $age = '' ) {
$this->BASE_CACHE = WP_CONTENT_DIR . '/cache';
if ( $base ) {
$this->BASE_CACHE = $base;
}
if ( $age ) {
$this->MAX_AGE = $age;
}
}
/**
* PHP4 constructor.
*/
public function RSSCache( $base = '', $age = '' ) {
self::__construct( $base, $age );
}
/*=======================================================================*
Function: set
Purpose: add an item to the cache, keyed on url
Input: url from which the rss file was fetched
Output: true on success
*=======================================================================*/
function set ($url, $rss) {
$cache_option = 'rss_' . $this->file_name( $url );
set_transient($cache_option, $rss, $this->MAX_AGE);
return $cache_option;
}
/*=======================================================================*
Function: get
Purpose: fetch an item from the cache
Input: url from which the rss file was fetched
Output: cached object on HIT, false on MISS
*=======================================================================*/
function get ($url) {
$this->ERROR = "";
$cache_option = 'rss_' . $this->file_name( $url );
if ( ! $rss = get_transient( $cache_option ) ) {
$this->debug(
"Cache does not contain: $url (cache option: $cache_option)"
);
return 0;
}
return $rss;
}
/*=======================================================================*
Function: check_cache
Purpose: check a url for membership in the cache
and whether the object is older then MAX_AGE (ie. STALE)
Input: url from which the rss file was fetched
Output: cached object on HIT, false on MISS
*=======================================================================*/
function check_cache ( $url ) {
$this->ERROR = "";
$cache_option = 'rss_' . $this->file_name( $url );
if ( get_transient($cache_option) ) {
// object exists and is current
return 'HIT';
} else {
// object does not exist
return 'MISS';
}
}
/*=======================================================================*
Function: serialize
*=======================================================================*/
function serialize ( $rss ) {
return serialize( $rss );
}
/*=======================================================================*
Function: unserialize
*=======================================================================*/
function unserialize ( $data ) {
return unserialize( $data );
}
/*=======================================================================*
Function: file_name
Purpose: map url to location in cache
Input: url from which the rss file was fetched
Output: a file name
*=======================================================================*/
function file_name ($url) {
return md5( $url );
}
/*=======================================================================*
Function: error
Purpose: register error
*=======================================================================*/
function error ($errormsg, $lvl=E_USER_WARNING) {
$this->ERROR = $errormsg;
if ( MAGPIE_DEBUG ) {
wp_trigger_error( '', $errormsg, $lvl);
}
else {
error_log( $errormsg, 0);
}
}
function debug ($debugmsg, $lvl=E_USER_NOTICE) {
if ( MAGPIE_DEBUG ) {
$this->error("MagpieRSS [debug] $debugmsg", $lvl);
}
}
}