2012-04-25 46 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");