00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010 class CachedBlock extends BlockBase {
00011
00012
00013
00014 protected $delegate;
00015
00016
00017
00018 protected $cache_id;
00019 protected $life_time;
00020 protected $cache_read = false;
00021
00022
00023
00024
00025
00026
00027
00028
00029 public function __construct($cache_id, $delegate, $life_time = GyroDate::ONE_DAY) {
00030 $this->delegate = $delegate;
00031 $this->life_time = $life_time;
00032 $this->cache_id = Arr::force($cache_id, false);
00033 array_unshift($this->cache_id, 'block');
00034
00035 parent::__construct('', '', '', $delegate->get_index(), $delegate->get_position());
00036 }
00037
00038
00039
00040
00041
00042
00043 protected function read_cache() {
00044 if (!$this->cache_read) {
00045 $this->cache_read = true;
00046 $content = '';
00047 $data = array();
00048 DB::start_trans();
00049 $cache = Cache::read($this->cache_id);
00050 if ($cache) {
00051 $content = $cache->get_content_plain();
00052 $data = $cache->get_data();
00053 }
00054 else {
00055
00056 $content = $this->delegate->get_content();
00057 $data = array(
00058 'title' => $this->delegate->get_title(),
00059 'name' => $this->delegate->get_name()
00060 );
00061 Cache::store($this->cache_id, $content, $this->life_time, $data, false);
00062 }
00063 DB::commit();
00064 $this->set_content($content);
00065 $this->set_title(Arr::get_item($data, 'title', ''));
00066 $this->set_name(Arr::get_item($data, 'name', ''));
00067 }
00068 }
00069
00070
00071
00072
00073
00074
00075 public function get_title() {
00076 $this->read_cache();
00077 return parent::get_title();
00078 }
00079
00080
00081
00082
00083
00084
00085 public function get_content() {
00086 $this->read_cache();
00087 return parent::get_content();
00088 }
00089
00090
00091
00092
00093
00094
00095 public function get_name() {
00096 $this->read_cache();
00097 return parent::get_name();
00098 }
00099 }