libs/sysplugins/smarty_cacheresource_keyvaluestore.php Quellcode

smarty_cacheresource_keyvaluestore.php
gehe zur Dokumentation dieser Datei
1 <?php
32 {
38  protected $contents = array();
39 
45  protected $timestamps = array();
46 
55  public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
56  {
57  $cached->filepath = $_template->source->uid . '#' . $this->sanitize($cached->source->resource) . '#' . $this->sanitize($cached->cache_id) . '#' . $this->sanitize($cached->compile_id);
58 
59  $this->populateTimestamp($cached);
60  }
61 
69  public function populateTimestamp(Smarty_Template_Cached $cached)
70  {
71  if (!$this->fetch($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id, $content, $timestamp, $cached->source->uid)) {
72  return;
73  }
74  $cached->content = $content;
75  $cached->timestamp = (int) $timestamp;
76  $cached->exists = $cached->timestamp;
77  }
78 
87  public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null)
88  {
89  if (!$cached) {
90  $cached = $_template->cached;
91  }
92  $content = $cached->content ? $cached->content : null;
93  $timestamp = $cached->timestamp ? $cached->timestamp : null;
94  if ($content === null || !$timestamp) {
95  if (!$this->fetch($_template->cached->filepath, $_template->source->name, $_template->cache_id, $_template->compile_id, $content, $timestamp, $_template->source->uid)) {
96  return false;
97  }
98  }
99  if (isset($content)) {
103  $_smarty_tpl = $_template;
104  eval("?>" . $content);
105 
106  return true;
107  }
108 
109  return false;
110  }
111 
120  public function writeCachedContent(Smarty_Internal_Template $_template, $content)
121  {
122  $this->addMetaTimestamp($content);
123 
124  return $this->write(array($_template->cached->filepath => $content), $_template->properties['cache_lifetime']);
125  }
126 
134  public function readCachedContent(Smarty_Internal_Template $_template)
135  {
136  $content = $_template->cached->content ? $_template->cached->content : null;
137  $timestamp = null;
138  if ($content === null) {
139  if (!$this->fetch($_template->cached->filepath, $_template->source->name, $_template->cache_id, $_template->compile_id, $content, $timestamp, $_template->source->uid)) {
140  return false;
141  }
142  }
143  if (isset($content)) {
144  return $content;
145  }
146  return false;
147  }
148 
160  public function clearAll(Smarty $smarty, $exp_time = null)
161  {
162  if (!$this->purge()) {
163  $this->invalidate(null);
164  }
165 
166  return - 1;
167  }
168 
184  public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
185  {
186  $uid = $this->getTemplateUid($smarty, $resource_name, $cache_id, $compile_id);
187  $cid = $uid . '#' . $this->sanitize($resource_name) . '#' . $this->sanitize($cache_id) . '#' . $this->sanitize($compile_id);
188  $this->delete(array($cid));
189  $this->invalidate($cid, $resource_name, $cache_id, $compile_id, $uid);
190 
191  return - 1;
192  }
193 
204  protected function getTemplateUid(Smarty $smarty, $resource_name, $cache_id, $compile_id)
205  {
206  $uid = '';
207  if (isset($resource_name)) {
208  $tpl = new $smarty->template_class($resource_name, $smarty);
209  if ($tpl->source->exists) {
210  $uid = $tpl->source->uid;
211  }
212 
213  // remove from template cache
214  if ($smarty->allow_ambiguous_resources) {
215  $_templateId = $tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id;
216  } else {
217  $_templateId = $smarty->joined_template_dir . '#' . $resource_name . $tpl->cache_id . $tpl->compile_id;
218  }
219  if (isset($_templateId[150])) {
220  $_templateId = sha1($_templateId);
221  }
222  unset($smarty->template_objects[$_templateId]);
223  }
224 
225  return $uid;
226  }
227 
235  protected function sanitize($string)
236  {
237  // some poeple smoke bad weed
238  $string = trim($string, '|');
239  if (!$string) {
240  return null;
241  }
242 
243  return preg_replace('#[^\w\|]+#S', '_', $string);
244  }
245 
259  protected function fetch($cid, $resource_name = null, $cache_id = null, $compile_id = null, &$content = null, &$timestamp = null, $resource_uid = null)
260  {
261  $t = $this->read(array($cid));
262  $content = !empty($t[$cid]) ? $t[$cid] : null;
263  $timestamp = null;
264 
265  if ($content && ($timestamp = $this->getMetaTimestamp($content))) {
266  $invalidated = $this->getLatestInvalidationTimestamp($cid, $resource_name, $cache_id, $compile_id, $resource_uid);
267  if ($invalidated > $timestamp) {
268  $timestamp = null;
269  $content = null;
270  }
271  }
272 
273  return !!$content;
274  }
275 
282  protected function addMetaTimestamp(&$content)
283  {
284  $mt = explode(" ", microtime());
285  $ts = pack("NN", $mt[1], (int) ($mt[0] * 100000000));
286  $content = $ts . $content;
287  }
288 
296  protected function getMetaTimestamp(&$content)
297  {
298  extract(unpack('N1s/N1m/a*content', $content));
299  return $s + ($m / 100000000);
300  }
301 
313  protected function invalidate($cid = null, $resource_name = null, $cache_id = null, $compile_id = null, $resource_uid = null)
314  {
315  $now = microtime(true);
316  $key = null;
317  // invalidate everything
318  if (!$resource_name && !$cache_id && !$compile_id) {
319  $key = 'IVK#ALL';
320  } // invalidate all caches by template
321  else {
322  if ($resource_name && !$cache_id && !$compile_id) {
323  $key = 'IVK#TEMPLATE#' . $resource_uid . '#' . $this->sanitize($resource_name);
324  } // invalidate all caches by cache group
325  else {
326  if (!$resource_name && $cache_id && !$compile_id) {
327  $key = 'IVK#CACHE#' . $this->sanitize($cache_id);
328  } // invalidate all caches by compile id
329  else {
330  if (!$resource_name && !$cache_id && $compile_id) {
331  $key = 'IVK#COMPILE#' . $this->sanitize($compile_id);
332  } // invalidate by combination
333  else {
334  $key = 'IVK#CID#' . $cid;
335  }
336  }
337  }
338  }
339  $this->write(array($key => $now));
340  }
341 
353  protected function getLatestInvalidationTimestamp($cid, $resource_name = null, $cache_id = null, $compile_id = null, $resource_uid = null)
354  {
355  // abort if there is no CacheID
356  if (false && !$cid) {
357  return 0;
358  }
359  // abort if there are no InvalidationKeys to check
360  if (!($_cid = $this->listInvalidationKeys($cid, $resource_name, $cache_id, $compile_id, $resource_uid))) {
361  return 0;
362  }
363 
364  // there are no InValidationKeys
365  if (!($values = $this->read($_cid))) {
366  return 0;
367  }
368  // make sure we're dealing with floats
369  $values = array_map('floatval', $values);
370 
371  return max($values);
372  }
373 
387  protected function listInvalidationKeys($cid, $resource_name = null, $cache_id = null, $compile_id = null, $resource_uid = null)
388  {
389  $t = array('IVK#ALL');
390  $_name = $_compile = '#';
391  if ($resource_name) {
392  $_name .= $resource_uid . '#' . $this->sanitize($resource_name);
393  $t[] = 'IVK#TEMPLATE' . $_name;
394  }
395  if ($compile_id) {
396  $_compile .= $this->sanitize($compile_id);
397  $t[] = 'IVK#COMPILE' . $_compile;
398  }
399  $_name .= '#';
400  // some poeple smoke bad weed
401  $cid = trim($cache_id, '|');
402  if (!$cid) {
403  return $t;
404  }
405  $i = 0;
406  while (true) {
407  // determine next delimiter position
408  $i = strpos($cid, '|', $i);
409  // add complete CacheID if there are no more delimiters
410  if ($i === false) {
411  $t[] = 'IVK#CACHE#' . $cid;
412  $t[] = 'IVK#CID' . $_name . $cid . $_compile;
413  $t[] = 'IVK#CID' . $_name . $_compile;
414  break;
415  }
416  $part = substr($cid, 0, $i);
417  // add slice to list
418  $t[] = 'IVK#CACHE#' . $part;
419  $t[] = 'IVK#CID' . $_name . $part . $_compile;
420  // skip past delimiter position
421  $i ++;
422  }
423 
424  return $t;
425  }
426 
435  public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
436  {
437  $key = 'LOCK#' . $cached->filepath;
438  $data = $this->read(array($key));
439 
440  return $data && time() - $data[$key] < $smarty->locking_timeout;
441  }
442 
451  public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
452  {
453  $cached->is_locked = true;
454  $key = 'LOCK#' . $cached->filepath;
455  $this->write(array($key => time()), $smarty->locking_timeout);
456  }
457 
466  public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
467  {
468  $cached->is_locked = false;
469  $key = 'LOCK#' . $cached->filepath;
470  $this->delete(array($key));
471  }
472 
480  abstract protected function read(array $keys);
481 
490  abstract protected function write(array $keys, $expire = null);
491 
499  abstract protected function delete(array $keys);
500 
506  protected function purge()
507  {
508  return false;
509  }
510 }




Korrekturen, Hinweise und Ergänzungen

Bitte scheuen Sie sich nicht und melden Sie, was auf dieser Seite sachlich falsch oder irreführend ist, was ergänzt werden sollte, was fehlt usw. Dazu bitte oben aus dem Menü Seite den Eintrag Support Forum wählen. Es ist eine kostenlose Anmeldung erforderlich, um Anmerkungen zu posten. Unpassende Postings, Spam usw. werden kommentarlos entfernt.