nyroFwk  0.2
cache/file.class.php
Go to the documentation of this file.
1 <?php
10 class cache_file extends cache_abstract {
11 
17  protected $prmVar;
18 
24  protected $prmOut;
25 
31  private $obSave;
32 
53  public function get(&$value, array $prm) {
54  if ($this->isEnabled() &&
55  config::initTab($prm, array(
56  'ttl'=>$this->cfg->ttl,
57  'id'=>null,
58  'tags'=>$this->cfg->tags,
59  'request'=>$this->cfg->request,
60  'serialize'=>$this->cfg->serialize
61  ))) {
62  $prm['value'] = &$value;
63  $prm['file'] = $this->file(array_merge($prm,
64  array('callFrom'=>debug::callFrom(1),'type'=>'get')));
65  $this->prmVar = $prm;
66  if (file::exists($prm['file'])) {
67  $date = file::date($prm['file']);
68  if ($prm['ttl'] == 0 || $date + $prm['ttl'] * 60 > time()) {
69  if ($prm['serialize'])
70  $value = unserialize(file::read($prm['file']));
71  else
72  eval('$value = '.file::read($prm['file']).';');
73  return $date;
74  }
75  }
76  }
77  return false;
78  }
79 
87  public function save() {
88  $ret = null;
89  if (!empty($this->prmVar))
90  if ($this->prmVar['serialize'])
91  $ret = file::write($this->prmVar['file'], serialize($this->prmVar['value']));
92  else
93  $ret = file::write($this->prmVar['file'], var_export($this->prmVar['value'], true));
94  $this->prmVar = null;
95  return $ret;
96  }
97 
116  public function start(array $prm) {
117  if ($this->isEnabled() && config::initTab($prm, array(
118  'ttl'=>$this->cfg->ttl,
119  'id'=>null,
120  'tags'=>$this->cfg->tags,
121  'request'=>$this->cfg->request
122  ))) {
123  $prm['file'] = $this->file(array_merge($prm,
124  array('callFrom'=>debug::callFrom(2),'type'=>'cache')));
125  if (file::exists($prm['file']) &&
126  ($prm['ttl'] == 0 ||
127  file::date($prm['file']) + $prm['ttl'] * 60 > time())) {
128  echo file::read($prm['file']);
129  return true;
130  } else {
131  $this->obSave = '';
132  /*
133  if (ob_get_length()) {
134  $this->obSave = ob_get_contents();
135  ob_end_clean();
136  }
137  // */
138  ob_start();
139  $this->prmOut = $prm;
140  }
141  }
142  return false;
143  }
144 
152  public function end() {
153  $ret = null;
154  if (!empty($this->prmOut)) {
155  $content = ob_get_contents();
156  ob_end_clean();
157  if ($this->obSave) {
158  echo $this->obSave.$content;
159  ob_start();
160  } else
161  echo $content;
162  $ret = file::write($this->prmOut['file'], $content);
163  }
164  $this->prmOut = null;
165  return $ret;
166 
167  }
168 
181  public function delete(array $prm = array()) {
182  if (config::initTab($prm, array(
183  'callFrom'=>'*',
184  'type'=>'*',
185  'id'=>'*',
186  'tags'=>false,
187  'request'=>array('uri'=>false,'meth'=>array())
188  ))) {
189  $file = $this->file($prm);
190  $file{strlen($file)-1} = '*';
191  if (!empty($prm['tags'])) {
192  for($i = 0; $i<count($prm['tags']); $i++) {
193  $file = str_replace(','.$prm['tags'][$i].',', '*,'.$prm['tags'][$i].',', $file);
194  }
195  }
196  $files = glob($file);
197  $nb = 0;
198  foreach($files as $f)
199  if (file::delete($f))
200  $nb++;
201  return $nb;
202  }
203  return 0;
204  }
205 
218  public function exists(array $prm) {
219  return file::exists($this->file($prm));
220  }
221 
222 
240  private function file(array $prm) {
241  $prm = array_merge(array(
242  'callFrom'=>null,
243  'type'=>null,
244  'id'=>null,
245  'tags'=>null,
246  'tagsS'=>null
247  ), $prm);
248  if (!empty($prm['tags'])) {
249  sort($prm['tags']);
250  $prm['tagsS'] = ',_,'.implode(',_,', $prm['tags']).',_,';
251  }
252  $fileA = array_merge($this->cfg->startFile, array(
253  $prm['callFrom'],
254  $prm['type'],
255  $prm['id'],
256  $prm['tagsS'],
257  null, //cache::idRequest($prm['request'])
258  ));
259  return str_replace(
260  array('-REQUEST::LANG-', '-REQUEST::OUT-'),
261  array(request::get('lang'), request::get('out')),
262  $this->cfg->path.implode('^', $fileA).'.cache');
263  }
264 
265 }
static callFrom($nb=1, $sep='-')
Definition: debug.class.php:38
start(array $prm)
static get($get=null)
static read($file)
Definition: file.class.php:242
exists(array $prm)
static exists($file)
Definition: file.class.php:97
static initTab(array &$vars, array $init)
static date($file)
Definition: file.class.php:302
static delete($file)
Definition: file.class.php:355
static write($file, $content)
Definition: file.class.php:256
file(array $prm)
$f
Definition: list.php:6
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13