libs/sysplugins/smarty_cacheresource_custom.php Quellcode

smarty_cacheresource_custom.php
gehe zur Dokumentation dieser Datei
1 <?php
17 {
30  abstract protected function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime);
31 
44  protected function fetchTimestamp($id, $name, $cache_id, $compile_id)
45  {
46  return null;
47  }
48 
61  abstract protected function save($id, $name, $cache_id, $compile_id, $exp_time, $content);
62 
73  abstract protected function delete($name, $cache_id, $compile_id, $exp_time);
74 
83  public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
84  {
85  $_cache_id = isset($cached->cache_id) ? preg_replace('![^\w\|]+!', '_', $cached->cache_id) : null;
86  $_compile_id = isset($cached->compile_id) ? preg_replace('![^\w\|]+!', '_', $cached->compile_id) : null;
87  $path = $cached->source->filepath . $_cache_id . $_compile_id;
88  $cached->filepath = sha1($path);
89  if ($_template->smarty->cache_locking) {
90  $cached->lock_id = sha1('lock.' . $path);
91  }
92  $this->populateTimestamp($cached);
93  }
94 
102  public function populateTimestamp(Smarty_Template_Cached $cached)
103  {
104  $mtime = $this->fetchTimestamp($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id);
105  if ($mtime !== null) {
106  $cached->timestamp = $mtime;
107  $cached->exists = !!$cached->timestamp;
108 
109  return;
110  }
111  $timestamp = null;
112  $this->fetch($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id, $cached->content, $timestamp);
113  $cached->timestamp = isset($timestamp) ? $timestamp : false;
114  $cached->exists = !!$cached->timestamp;
115  }
116 
125  public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null)
126  {
127  if (!$cached) {
128  $cached = $_template->cached;
129  }
130  $content = $cached->content ? $cached->content : null;
131  $timestamp = $cached->timestamp ? $cached->timestamp : null;
132  if ($content === null || !$timestamp) {
133  $this->fetch(
134  $_template->cached->filepath,
135  $_template->source->name,
136  $_template->cache_id,
137  $_template->compile_id,
138  $content,
139  $timestamp
140  );
141  }
142  if (isset($content)) {
146  $_smarty_tpl = $_template;
147  eval("?>" . $content);
148 
149  return true;
150  }
151 
152  return false;
153  }
154 
163  public function writeCachedContent(Smarty_Internal_Template $_template, $content)
164  {
165  return $this->save(
166  $_template->cached->filepath,
167  $_template->source->name,
168  $_template->cache_id,
169  $_template->compile_id,
170  $_template->properties['cache_lifetime'],
171  $content
172  );
173  }
174 
182  public function readCachedContent(Smarty_Internal_Template $_template)
183  {
184  $content = $_template->cached->content ? $_template->cached->content : null;
185  $timestamp = null;
186  if ($content === null) {
187  $timestamp = null;
188  $this->fetch(
189  $_template->cached->filepath,
190  $_template->source->name,
191  $_template->cache_id,
192  $_template->compile_id,
193  $content,
194  $timestamp
195  );
196  }
197  if (isset($content)) {
198  return $content;
199  }
200  return false;
201  }
202 
211  public function clearAll(Smarty $smarty, $exp_time = null)
212  {
213  $this->cache = array();
214 
215  return $this->delete(null, null, null, $exp_time);
216  }
217 
229  public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
230  {
231  $this->cache = array();
232  $cache_name = null;
233 
234  if (isset($resource_name)) {
235  $_save_stat = $smarty->caching;
236  $smarty->caching = true;
237  $tpl = new $smarty->template_class($resource_name, $smarty);
238  $smarty->caching = $_save_stat;
239 
240  if ($tpl->source->exists) {
241  $cache_name = $tpl->source->name;
242  } else {
243  return 0;
244  }
245  // remove from template cache
246  if ($smarty->allow_ambiguous_resources) {
247  $_templateId = $tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id;
248  } else {
249  $_templateId = $smarty->joined_template_dir . '#' . $resource_name . $tpl->cache_id . $tpl->compile_id;
250  }
251  if (isset($_templateId[150])) {
252  $_templateId = sha1($_templateId);
253  }
254  unset($smarty->template_objects[$_templateId]);
255  // template object no longer needed
256  unset($tpl);
257  }
258 
259  return $this->delete($cache_name, $cache_id, $compile_id, $exp_time);
260  }
261 
270  public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
271  {
272  $id = $cached->lock_id;
273  $name = $cached->source->name . '.lock';
274 
275  $mtime = $this->fetchTimestamp($id, $name, $cached->cache_id, $cached->compile_id);
276  if ($mtime === null) {
277  $this->fetch($id, $name, $cached->cache_id, $cached->compile_id, $content, $mtime);
278  }
279  return $mtime && ($t = time()) - $mtime < $smarty->locking_timeout;
280  }
281 
290  public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
291  {
292  $cached->is_locked = true;
293  $id = $cached->lock_id;
294  $name = $cached->source->name . '.lock';
295  $this->save($id, $name, $cached->cache_id, $cached->compile_id, $smarty->locking_timeout, '');
296  }
297 
306  public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
307  {
308  $cached->is_locked = false;
309  $name = $cached->source->name . '.lock';
310  $this->delete($name, $cached->cache_id, $cached->compile_id, null);
311  }
312 }




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.