PEEL Shopping
Open source ecommerce : PEEL Shopping
smarty_cacheresource.php
Go to the documentation of this file.
1 <?php
16 abstract class Smarty_CacheResource {
21  public static $resources = array();
22 
27  protected static $sysplugins = array(
28  'file' => true,
29  );
30 
38  public abstract function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template);
39 
46  public abstract function populateTimestamp(Smarty_Template_Cached $cached);
47 
55  public abstract function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached=null);
56 
64  public abstract function writeCachedContent(Smarty_Internal_Template $_template, $content);
65 
72  public function getCachedContent(Smarty_Internal_Template $_template)
73  {
74  if ($_template->cached->handler->process($_template)) {
75  ob_start();
76  $_template->properties['unifunc']($_template);
77  return ob_get_clean();
78  }
79  return null;
80  }
81 
89  public abstract function clearAll(Smarty $smarty, $exp_time=null);
90 
101  public abstract function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time);
102 
103 
104  public function locked(Smarty $smarty, Smarty_Template_Cached $cached)
105  {
106  // theoretically locking_timeout should be checked against time_limit (max_execution_time)
107  $start = microtime(true);
108  $hadLock = null;
109  while ($this->hasLock($smarty, $cached)) {
110  $hadLock = true;
111  if (microtime(true) - $start > $smarty->locking_timeout) {
112  // abort waiting for lock release
113  return false;
114  }
115  sleep(1);
116  }
117  return $hadLock;
118  }
119 
120  public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
121  {
122  // check if lock exists
123  return false;
124  }
125 
126  public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
127  {
128  // create lock
129  return true;
130  }
131 
132  public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
133  {
134  // release lock
135  return true;
136  }
137 
138 
146  public static function load(Smarty $smarty, $type = null)
147  {
148  if (!isset($type)) {
149  $type = $smarty->caching_type;
150  }
151 
152  // try smarty's cache
153  if (isset($smarty->_cacheresource_handlers[$type])) {
154  return $smarty->_cacheresource_handlers[$type];
155  }
156 
157  // try registered resource
158  if (isset($smarty->registered_cache_resources[$type])) {
159  // do not cache these instances as they may vary from instance to instance
160  return $smarty->_cacheresource_handlers[$type] = $smarty->registered_cache_resources[$type];
161  }
162  // try sysplugins dir
163  if (isset(self::$sysplugins[$type])) {
164  if (!isset(self::$resources[$type])) {
165  $cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($type);
166  self::$resources[$type] = new $cache_resource_class();
167  }
168  return $smarty->_cacheresource_handlers[$type] = self::$resources[$type];
169  }
170  // try plugins dir
171  $cache_resource_class = 'Smarty_CacheResource_' . ucfirst($type);
172  if ($smarty->loadPlugin($cache_resource_class)) {
173  if (!isset(self::$resources[$type])) {
174  self::$resources[$type] = new $cache_resource_class();
175  }
176  return $smarty->_cacheresource_handlers[$type] = self::$resources[$type];
177  }
178  // give up
179  throw new SmartyException("Unable to load cache resource '{$type}'");
180  }
181 
187  public static function invalidLoadedCache(Smarty $smarty)
188  {
189  foreach ($smarty->template_objects as $tpl) {
190  if (isset($tpl->cached)) {
191  $tpl->cached->valid = false;
192  $tpl->cached->processed = false;
193  }
194  }
195  }
196 }
197 
212  public $filepath = false;
213 
218  public $content = null;
219 
224  public $timestamp = false;
225 
230  public $exists = false;
231 
236  public $valid = false;
237 
242  public $processed = false;
243 
248  public $handler = null;
249 
254  public $compile_id = null;
255 
260  public $cache_id = null;
261 
266  public $lock_id = null;
267 
272  public $is_locked = false;
273 
278  public $source = null;
279 
285  public function __construct(Smarty_Internal_Template $_template)
286  {
287  $this->compile_id = $_template->compile_id;
288  $this->cache_id = $_template->cache_id;
289  $this->source = $_template->source;
290  $_template->cached = $this;
291  $smarty = $_template->smarty;
292 
293  //
294  // load resource handler
295  //
296  $this->handler = $handler = Smarty_CacheResource::load($smarty); // Note: prone to circular references
297 
298  //
299  // check if cache is valid
300  //
301  if (!($_template->caching == Smarty::CACHING_LIFETIME_CURRENT || $_template->caching == Smarty::CACHING_LIFETIME_SAVED) || $_template->source->recompiled) {
302  $handler->populate($this, $_template);
303  return;
304  }
305  while (true) {
306  while (true) {
307  $handler->populate($this, $_template);
308  if ($this->timestamp === false || $smarty->force_compile || $smarty->force_cache) {
309  $this->valid = false;
310  } else {
311  $this->valid = true;
312  }
313  if ($this->valid && $_template->caching == Smarty::CACHING_LIFETIME_CURRENT && $_template->cache_lifetime >= 0 && time() > ($this->timestamp + $_template->cache_lifetime)) {
314  // lifetime expired
315  $this->valid = false;
316  }
317  if ($this->valid || !$_template->smarty->cache_locking) {
318  break;
319  }
320  if (!$this->handler->locked($_template->smarty, $this)) {
321  $this->handler->acquireLock($_template->smarty, $this);
322  break 2;
323  }
324  }
325  if ($this->valid) {
326  if (!$_template->smarty->cache_locking || $this->handler->locked($_template->smarty, $this) === null) {
327  // load cache file for the following checks
328  if ($smarty->debugging) {
330  }
331  if($handler->process($_template, $this) === false) {
332  $this->valid = false;
333  } else {
334  $this->processed = true;
335  }
336  if ($smarty->debugging) {
338  }
339  } else {
340  continue;
341  }
342  } else {
343  return;
344  }
345  if ($this->valid && $_template->caching === Smarty::CACHING_LIFETIME_SAVED && $_template->properties['cache_lifetime'] >= 0 && (time() > ($_template->cached->timestamp + $_template->properties['cache_lifetime']))) {
346  $this->valid = false;
347  }
348  if (!$this->valid && $_template->smarty->cache_locking) {
349  $this->handler->acquireLock($_template->smarty, $this);
350  return;
351  } else {
352  return;
353  }
354  }
355  }
356 
364  public function write(Smarty_Internal_Template $_template, $content)
365  {
366  if (!$_template->source->recompiled) {
367  if ($this->handler->writeCachedContent($_template, $content)) {
368  $this->timestamp = time();
369  $this->exists = true;
370  $this->valid = true;
371  if ($_template->smarty->cache_locking) {
372  $this->handler->releaseLock($_template->smarty, $this);
373  }
374  return true;
375  }
376  }
377  return false;
378  }
379 
380 }
381 ?>
releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
static load(Smarty $smarty, $type=null)
Load Cache Resource Handler.
loadPlugin($plugin_name, $check=true)
Takes unknown classes and loads plugin files for them class name format: Smarty_PluginType_PluginName...
static start_cache($template)
Start logging of cache time.
static invalidLoadedCache(Smarty $smarty)
Invalid Loaded Cache Files.
__construct(Smarty_Internal_Template $_template)
create Cached Object container
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
static end_cache($template)
End logging of cache time.
$start
Definition: attributs.php:22
const CACHING_LIFETIME_CURRENT
Smarty plugin to format text blocks.
populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
populate Cached Object with meta data from Resource
process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached=null)
Read the cached template and process header.
write(Smarty_Internal_Template $_template, $content)
Write this cache object to handler.
locked(Smarty $smarty, Smarty_Template_Cached $cached)
populateTimestamp(Smarty_Template_Cached $cached)
populate Cached Object with timestamp and exists from Resource
const CACHING_LIFETIME_SAVED
hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
getCachedContent(Smarty_Internal_Template $_template)
Return cached content.
clearAll(Smarty $smarty, $exp_time=null)
Empty cache.
writeCachedContent(Smarty_Internal_Template $_template, $content)
Write the rendered template output to cache.
clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
Empty cache for a specific template.
acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)

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.