Skip to content

Commit

Permalink
Made it possible to set the cache lifetime per RSS source.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajshort committed Jan 28, 2011
1 parent 99d7007 commit 577daf5
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions code/model/RssContentSource.php
Expand Up @@ -12,8 +12,15 @@
*/
class RssContentSource extends ExternalContentSource {

const DEFAULT_CACHE_LIFETIME = 3600;

public static $db = array(
'Url' => 'Varchar(255)'
'Url' => 'Varchar(255)',
'CacheLifetime' => 'Int'
);

public static $defaults = array(
'CacheLifetime' => self::DEFAULT_CACHE_LIFETIME
);

protected $client;
Expand All @@ -23,9 +30,12 @@ public function getCMSFields() {
Requirements::css('rssconnector/css/RssContentAdmin.css');

$fields->addFieldToTab(
'Root.Main', new TextField('Url', 'RSS/Atom Feed URL'),
'ShowContentInMenu'
);
'Root.Main',
new TextField('Url', 'RSS/Atom Feed URL'), 'ShowContentInMenu');

$fields->addFieldToTab(
'Root.Advanced',
new NumericField('CacheLifetime', 'Cache Lifetime (in seconds)'));

if (!$this->Url || !$client = $this->getClient()) {
return $fields;
Expand Down Expand Up @@ -91,6 +101,7 @@ public function getClient() {
if (!$this->client) {
$this->client = new SimplePie($this->Url);
$this->client->enable_cache(true);
$this->client->set_cache_duration($this->getCacheLifetime());
$this->client->set_cache_location(TEMP_FOLDER);
}

Expand All @@ -101,4 +112,11 @@ public function canImport() {
return $this->Url && !$this->getClient()->error;
}

/**
* @return int
*/
public function getCacheLifetime() {
return ($t = $this->getField('CacheLifetime')) ? $t : self::DEFAULT_CACHE_LIFETIME;
}

}

0 comments on commit 577daf5

Please sign in to comment.