PEEL Shopping
Open source ecommerce : PEEL Shopping
smarty_cacheresource_keyvaluestore.php
Go to the documentation of this file.
1 <?php
35 
40  protected $contents = array();
45  protected $timestamps = array();
46 
54  public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
55  {
56  $cached->filepath = $_template->source->uid
57  . '#' . $this->sanitize($cached->source->name)
58  . '#' . $this->sanitize($cached->cache_id)
59  . '#' . $this->sanitize($cached->compile_id);
60 
61  $this->populateTimestamp($cached);
62  }
63 
70  public function populateTimestamp(Smarty_Template_Cached $cached)
71  {
72  if (!$this->fetch($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id, $content, $timestamp, $cached->source->uid)) {
73  return;
74  }
75  $cached->content = $content;
76  $cached->timestamp = (int) $timestamp;
77  $cached->exists = $cached->timestamp;
78  }
79 
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)) {
100  $_smarty_tpl = $_template;
101  eval("?>" . $content);
102  return true;
103  }
104  return false;
105  }
106 
114  public function writeCachedContent(Smarty_Internal_Template $_template, $content)
115  {
116  $this->addMetaTimestamp($content);
117  return $this->write(array($_template->cached->filepath => $content), $_template->properties['cache_lifetime']);
118  }
119 
131  public function clearAll(Smarty $smarty, $exp_time=null)
132  {
133  if (!$this->purge()) {
134  $this->invalidate(null);
135  }
136  return -1;
137  }
138 
154  public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
155  {
156  $uid = $this->getTemplateUid($smarty, $resource_name, $cache_id, $compile_id);
157  $cid = $uid . '#' . $this->sanitize($resource_name) . '#' . $this->sanitize($cache_id) . '#' . $this->sanitize($compile_id);
158  $this->delete(array($cid));
159  $this->invalidate($cid, $resource_name, $cache_id, $compile_id, $uid);
160  return -1;
161  }
171  protected function getTemplateUid(Smarty $smarty, $resource_name, $cache_id, $compile_id)
172  {
173  $uid = '';
174  if (isset($resource_name)) {
175  $tpl = new $smarty->template_class($resource_name, $smarty);
176  if ($tpl->source->exists) {
177  $uid = $tpl->source->uid;
178  }
179 
180  // remove from template cache
181  if ($smarty->allow_ambiguous_resources) {
182  $_templateId = $tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id;
183  } else {
184  $_templateId = $smarty->joined_template_dir . '#' . $resource_name . $tpl->cache_id . $tpl->compile_id;
185  }
186  if (isset($_templateId[150])) {
187  $_templateId = sha1($_templateId);
188  }
189  unset($smarty->template_objects[$_templateId]);
190  }
191  return $uid;
192  }
193 
200  protected function sanitize($string)
201  {
202  // some poeple smoke bad weed
203  $string = trim($string, '|');
204  if (!$string) {
205  return null;
206  }
207  return preg_replace('#[^\w\|]+#S', '_', $string);
208  }
209 
222  protected function fetch($cid, $resource_name = null, $cache_id = null, $compile_id = null, &$content = null, &$timestamp = null, $resource_uid = null)
223  {
224  $t = $this->read(array($cid));
225  $content = !empty($t[$cid]) ? $t[$cid] : null;
226  $timestamp = null;
227 
228  if ($content && ($timestamp = $this->getMetaTimestamp($content))) {
229  $invalidated = $this->getLatestInvalidationTimestamp($cid, $resource_name, $cache_id, $compile_id, $resource_uid);
230  if ($invalidated > $timestamp) {
231  $timestamp = null;
232  $content = null;
233  }
234  }
235 
236  return !!$content;
237  }
238 
246  protected function addMetaTimestamp(&$content)
247  {
248  $mt = explode(" ", microtime());
249  $ts = pack("NN", $mt[1], (int) ($mt[0] * 100000000));
250  $content = $ts . $content;
251  }
252 
259  protected function getMetaTimestamp(&$content)
260  {
261  $s = unpack("N", substr($content, 0, 4));
262  $m = unpack("N", substr($content, 4, 4));
263  $content = substr($content, 8);
264  return $s[1] + ($m[1] / 100000000);
265  }
266 
277  protected function invalidate($cid = null, $resource_name = null, $cache_id = null, $compile_id = null, $resource_uid = null)
278  {
279  $now = microtime(true);
280  $key = null;
281  // invalidate everything
282  if (!$resource_name && !$cache_id && !$compile_id) {
283  $key = 'IVK#ALL';
284  }
285  // invalidate all caches by template
286  else if ($resource_name && !$cache_id && !$compile_id) {
287  $key = 'IVK#TEMPLATE#' . $resource_uid . '#' . $this->sanitize($resource_name);
288  }
289  // invalidate all caches by cache group
290  else if (!$resource_name && $cache_id && !$compile_id) {
291  $key = 'IVK#CACHE#' . $this->sanitize($cache_id);
292  }
293  // invalidate all caches by compile id
294  else if (!$resource_name && !$cache_id && $compile_id) {
295  $key = 'IVK#COMPILE#' . $this->sanitize($compile_id);
296  }
297  // invalidate by combination
298  else {
299  $key = 'IVK#CID#' . $cid;
300  }
301  $this->write(array($key => $now));
302  }
303 
314  protected function getLatestInvalidationTimestamp($cid, $resource_name = null, $cache_id = null, $compile_id = null, $resource_uid = null)
315  {
316  // abort if there is no CacheID
317  if (false && !$cid) {
318  return 0;
319  }
320  // abort if there are no InvalidationKeys to check
321  if (!($_cid = $this->listInvalidationKeys($cid, $resource_name, $cache_id, $compile_id, $resource_uid))) {
322  return 0;
323  }
324 
325  // there are no InValidationKeys
326  if (!($values = $this->read($_cid))) {
327  return 0;
328  }
329  // make sure we're dealing with floats
330  $values = array_map('floatval', $values);
331  return max($values);
332  }
333 
347  protected function listInvalidationKeys($cid, $resource_name = null, $cache_id = null, $compile_id = null, $resource_uid = null)
348  {
349  $t = array('IVK#ALL');
350  $_name = $_compile = '#';
351  if ($resource_name) {
352  $_name .= $resource_uid . '#' . $this->sanitize($resource_name);
353  $t[] = 'IVK#TEMPLATE' . $_name;
354  }
355  if ($compile_id) {
356  $_compile .= $this->sanitize($compile_id);
357  $t[] = 'IVK#COMPILE' . $_compile;
358  }
359  $_name .= '#';
360  // some poeple smoke bad weed
361  $cid = trim($cache_id, '|');
362  if (!$cid) {
363  return $t;
364  }
365  $i = 0;
366  while (true) {
367  // determine next delimiter position
368  $i = strpos($cid, '|', $i);
369  // add complete CacheID if there are no more delimiters
370  if ($i === false) {
371  $t[] = 'IVK#CACHE#' . $cid;
372  $t[] = 'IVK#CID' . $_name . $cid . $_compile;
373  $t[] = 'IVK#CID' . $_name . $_compile;
374  break;
375  }
376  $part = substr($cid, 0, $i);
377  // add slice to list
378  $t[] = 'IVK#CACHE#' . $part;
379  $t[] = 'IVK#CID' . $_name . $part . $_compile;
380  // skip past delimiter position
381  $i++;
382  }
383  return $t;
384  }
385 
393  public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
394  {
395  $key = 'LOCK#' . $cached->filepath;
396  $data = $this->read(array($key));
397  return $data && time() - $data[$key] < $smarty->locking_timeout;
398  }
399 
406  public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
407  {
408  $cached->is_locked = true;
409  $key = 'LOCK#' . $cached->filepath;
410  $this->write(array($key => time()), $smarty->locking_timeout);
411  }
412 
419  public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
420  {
421  $cached->is_locked = false;
422  $key = 'LOCK#' . $cached->filepath;
423  $this->delete(array($key));
424  }
425 
432  protected abstract function read(array $keys);
433 
441  protected abstract function write(array $keys, $expire=null);
442 
449  protected abstract function delete(array $keys);
450 
456  protected function purge()
457  {
458  return false;
459  }
460 
461 }
462 
463 ?>
write(array $keys, $expire=null)
Save values for a set of keys to cache.
foreach(array('date1', 'date2', 'type', 'renewals', 'width') as $item) $data
Definition: chart-data.php:29
process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached=null)
Read the cached template and process the header.
addMetaTimestamp(&$content)
Add current microtime to the beginning of $cache_content.
read(array $keys)
Read values for a set of keys from cache.
writeCachedContent(Smarty_Internal_Template $_template, $content)
Write the rendered template output to cache.
if(!empty($_GET['id'])) if(isset($_POST['form_name'], $_POST['form_subject'], $_POST['form_text'], $_POST['form_lang'])&&empty($_GET['id'])) if(empty($_GET['id'])) $tpl
Smarty plugin to format text blocks.
listInvalidationKeys($cid, $resource_name=null, $cache_id=null, $compile_id=null, $resource_uid=null)
Translate a CacheID into the list of applicable InvalidationKeys.
getLatestInvalidationTimestamp($cid, $resource_name=null, $cache_id=null, $compile_id=null, $resource_uid=null)
Determine the latest timestamp known to the invalidation chain.
fetch($cid, $resource_name=null, $cache_id=null, $compile_id=null, &$content=null, &$timestamp=null, $resource_uid=null)
Fetch and prepare a cache object.
acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
Lock cache for this template.
releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
Unlock cache for this template.
clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
Empty cache for a specific template.
populateTimestamp(Smarty_Template_Cached $cached)
populate Cached Object with timestamp and exists from Resource
populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
populate Cached Object with meta data from Resource
getTemplateUid(Smarty $smarty, $resource_name, $cache_id, $compile_id)
Get template's unique ID.
getMetaTimestamp(&$content)
Extract the timestamp the $content was cached.
hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
Check is cache is locked for this template.
clearAll(Smarty $smarty, $exp_time=null)
Empty cache.
invalidate($cid=null, $resource_name=null, $cache_id=null, $compile_id=null, $resource_uid=null)
Invalidate CacheID.
sanitize($string)
Sanitize CacheID components.

This documentation for Open ecommerce PEEL Shopping and PEEL.fr has been generated by Doxygen on Thu Oct 15 2015 14:41:16 - Peel ecommerce is a product of Agence web Advisto SAS. All rights reserved.