diff options
Diffstat (limited to 'model/Relive.php')
-rw-r--r-- | model/Relive.php | 66 |
1 files changed, 61 insertions, 5 deletions
diff --git a/model/Relive.php b/model/Relive.php index 3ffdc1b..a76761c 100644 --- a/model/Relive.php +++ b/model/Relive.php @@ -2,9 +2,18 @@ class Relive extends ModelBase { - function relive_talks() + public function isEnabled() { - $talks = @file_get_contents(get('OVERVIEW.RELIVE_JSON')); + // having CONFERENCE.RELIVE is not enough! + return $this->has('CONFERENCE.RELIVE_JSON'); + } + + public function getTalks() + { + if($talks_by_id = $this->getCached()) + return $talks_by_id; + + $talks = file_get_contents($this->get('CONFERENCE.RELIVE_JSON')); $talks = utf8_decode($talks); $talks = (array)json_decode($talks, true); @@ -14,11 +23,58 @@ class Relive extends ModelBase }); $talks_by_id = array(); - foreach ($talks as $value) + foreach ($talks as $talk) { - $talks_by_id[$value['id']] = $value; + if($talk['status'] == 'released') + $talk['url'] = $talk['release_url']; + else + $talk['url'] = 'relive/'.rawurlencode($talk['id']).'/'; + + $talks_by_id[$talk['id']] = $talk; } - return $talks_by_id; + return $this->doCache($talks_by_id); + } + + public function getTalk($id) + { + $talks = $this->getTalks(); + if(!isset($talks[$id])) + throw new NotFoundException('Relive-Talk id '.$id); + + return $talks[$id]; + } + + private function isCacheEnabled() + { + return $this->has('CONFERENCE.RELIVE_JSON_CACHE') && function_exists('apc_fetch') && function_exists('apc_store'); + } + + private function getCacheDuration() + { + return $this->get('CONFERENCE.RELIVE_JSON_CACHE', 60*10 /* 10 minutes */); + } + + private $localCache = null; + private function getCached() + { + if($this->localCache) + return $this->localCache; + + if(!$this->isCacheEnabled()) + return null; + + return apc_fetch('RELIVE.CACHE'); + } + + private function doCache($value) + { + $this->localCache = $value; + + if(!$this->isCacheEnabled()) + return $value; + + apc_store('RELIVE.CACHE', $value, $this->getCacheDuration()); + return $value; } } |