00001 <?php
00010 class cache_file extends cache_abstract {
00011
00017 protected $prmVar;
00018
00024 protected $prmOut;
00025
00031 private $obSave;
00032
00053 public function get(&$value, array $prm) {
00054 if ($this->isEnabled() &&
00055 config::initTab($prm, array(
00056 'ttl'=>$this->cfg->ttl,
00057 'id'=>null,
00058 'tags'=>$this->cfg->tags,
00059 'request'=>$this->cfg->request,
00060 'serialize'=>$this->cfg->serialize
00061 ))) {
00062 $prm['value'] = &$value;
00063 $prm['file'] = $this->file(array_merge($prm,
00064 array('callFrom'=>debug::callFrom(1),'type'=>'get')));
00065 $this->prmVar = $prm;
00066 if (file::exists($prm['file']) &&
00067 ($prm['ttl'] == 0 || file::date($prm['file']) + $prm['ttl'] * 60 > time())) {
00068 if ($prm['serialize'])
00069 $value = unserialize(file::read($prm['file']));
00070 else
00071 eval('$value = '.file::read($prm['file']).';');
00072 return true;
00073 }
00074 }
00075 return false;
00076 }
00077
00085 public function save() {
00086 $ret = null;
00087 if (!empty($this->prmVar))
00088 if ($this->prmVar['serialize'])
00089 $ret = file::write($this->prmVar['file'], serialize($this->prmVar['value']));
00090 else
00091 $ret = file::write($this->prmVar['file'], var_export($this->prmVar['value'], true));
00092 $this->prmVar = null;
00093 return $ret;
00094 }
00095
00114 public function start(array $prm) {
00115 if ($this->isEnabled() && config::initTab($prm, array(
00116 'ttl'=>$this->cfg->ttl,
00117 'id'=>null,
00118 'tags'=>$this->cfg->tags,
00119 'request'=>$this->cfg->request
00120 ))) {
00121 $prm['file'] = $this->file(array_merge($prm,
00122 array('callFrom'=>debug::callFrom(2),'type'=>'cache')));
00123 if (file::exists($prm['file']) &&
00124 ($prm['ttl'] == 0 ||
00125 file::date($prm['file']) + $prm['ttl'] * 60 > time())) {
00126 echo file::read($prm['file']);
00127 return true;
00128 } else {
00129 $this->obSave = '';
00130
00131
00132
00133
00134
00135
00136 ob_start();
00137 $this->prmOut = $prm;
00138 }
00139 }
00140 return false;
00141 }
00142
00150 public function end() {
00151 $ret = null;
00152 if (!empty($this->prmOut)) {
00153 $content = ob_get_contents();
00154 ob_end_clean();
00155 if ($this->obSave) {
00156 echo $this->obSave.$content;
00157 ob_start();
00158 } else
00159 echo $content;
00160 $ret = file::write($this->prmOut['file'], $content);
00161 }
00162 $this->prmOut = null;
00163 return $ret;
00164
00165 }
00166
00179 public function delete(array $prm = array()) {
00180 if (config::initTab($prm, array(
00181 'callFrom'=>'*',
00182 'type'=>'*',
00183 'id'=>'*',
00184 'tags'=>false,
00185 'request'=>array('uri'=>false,'meth'=>array())
00186 ))) {
00187 $file = $this->file($prm);
00188 $file{strlen($file)-1} = '*';
00189 if (!empty($prm['tags'])) {
00190 for($i = 0; $i<count($prm['tags']); $i++) {
00191 $file = str_replace(','.$prm['tags'][$i].',', '*,'.$prm['tags'][$i].',', $file);
00192 }
00193 }
00194 $files = glob($file);
00195 $nb = 0;
00196 foreach($files as $f)
00197 if (file::delete($f))
00198 $nb++;
00199 return $nb;
00200 }
00201 return 0;
00202 }
00203
00216 public function exists(array $prm) {
00217 return file::exists($this->file($prm));
00218 }
00219
00220
00238 private function file(array $prm) {
00239 $prm = array_merge(array(
00240 'callFrom'=>null,
00241 'type'=>null,
00242 'id'=>null,
00243 'tags'=>null,
00244 'tagsS'=>null
00245 ), $prm);
00246 if (!empty($prm['tags'])) {
00247 sort($prm['tags']);
00248 $prm['tagsS'] = ',_,'.implode(',_,', $prm['tags']).',_,';
00249 }
00250 $fileA = array_merge($this->cfg->startFile, array(
00251 $prm['callFrom'],
00252 $prm['type'],
00253 $prm['id'],
00254 $prm['tagsS'],
00255 null,
00256 ));
00257 return str_replace(
00258 array('-REQUEST::LANG-', '-REQUEST::OUT-'),
00259 array(request::get('lang'), request::get('out')),
00260 $this->cfg->path.implode('^', $fileA).'.cache');
00261 }
00262
00263 }