57 $cached->filepath = $_template->source->uid .
'#' . $this->
sanitize($cached->source->resource) .
'#' . $this->
sanitize($cached->cache_id) .
'#' . $this->
sanitize($cached->compile_id);
71 if (!$this->
fetch($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id, $content, $timestamp, $cached->source->uid)) {
74 $cached->content = $content;
75 $cached->timestamp = (int) $timestamp;
76 $cached->exists = $cached->timestamp;
90 $cached = $_template->cached;
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)) {
99 if (isset($content)) {
103 $_smarty_tpl = $_template;
104 eval(
"?>" . $content);
124 return $this->
write(array($_template->cached->filepath => $content), $_template->properties[
'cache_lifetime']);
136 $content = $_template->cached->content ? $_template->cached->content : 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)) {
143 if (isset($content)) {
162 if (!$this->
purge()) {
184 public function clear(
Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
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);
207 if (isset($resource_name)) {
208 $tpl =
new $smarty->template_class($resource_name, $smarty);
209 if ($tpl->source->exists) {
210 $uid = $tpl->source->uid;
214 if ($smarty->allow_ambiguous_resources) {
215 $_templateId = $tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id;
217 $_templateId = $smarty->joined_template_dir .
'#' . $resource_name . $tpl->cache_id . $tpl->compile_id;
219 if (isset($_templateId[150])) {
220 $_templateId = sha1($_templateId);
222 unset($smarty->template_objects[$_templateId]);
238 $string = trim($string,
'|');
243 return preg_replace(
'#[^\w\|]+#S',
'_', $string);
259 protected function fetch($cid, $resource_name = null, $cache_id = null, $compile_id = null, &$content = null, &$timestamp = null, $resource_uid = null)
261 $t = $this->
read(array($cid));
262 $content = !empty($t[$cid]) ? $t[$cid] : null;
267 if ($invalidated > $timestamp) {
284 $mt = explode(
" ", microtime());
285 $ts = pack(
"NN", $mt[1], (
int) ($mt[0] * 100000000));
286 $content = $ts . $content;
298 extract(unpack(
'N1s/N1m/a*content', $content));
299 return $s + ($m / 100000000);
313 protected function invalidate($cid = null, $resource_name = null, $cache_id = null, $compile_id = null, $resource_uid = null)
315 $now = microtime(
true);
318 if (!$resource_name && !$cache_id && !$compile_id) {
322 if ($resource_name && !$cache_id && !$compile_id) {
323 $key =
'IVK#TEMPLATE#' . $resource_uid .
'#' . $this->
sanitize($resource_name);
326 if (!$resource_name && $cache_id && !$compile_id) {
327 $key =
'IVK#CACHE#' . $this->
sanitize($cache_id);
330 if (!$resource_name && !$cache_id && $compile_id) {
331 $key =
'IVK#COMPILE#' . $this->
sanitize($compile_id);
334 $key =
'IVK#CID#' . $cid;
339 $this->
write(array($key => $now));
356 if (
false && !$cid) {
360 if (!($_cid = $this->
listInvalidationKeys($cid, $resource_name, $cache_id, $compile_id, $resource_uid))) {
365 if (!($values = $this->
read($_cid))) {
369 $values = array_map(
'floatval', $values);
387 protected function listInvalidationKeys($cid, $resource_name = null, $cache_id = null, $compile_id = null, $resource_uid = null)
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;
396 $_compile .= $this->
sanitize($compile_id);
397 $t[] =
'IVK#COMPILE' . $_compile;
401 $cid = trim($cache_id,
'|');
408 $i = strpos($cid,
'|', $i);
411 $t[] =
'IVK#CACHE#' . $cid;
412 $t[] =
'IVK#CID' . $_name . $cid . $_compile;
413 $t[] =
'IVK#CID' . $_name . $_compile;
416 $part = substr($cid, 0, $i);
418 $t[] =
'IVK#CACHE#' . $part;
419 $t[] =
'IVK#CID' . $_name . $part . $_compile;
437 $key =
'LOCK#' . $cached->filepath;
438 $data = $this->
read(array($key));
440 return $data && time() - $data[$key] < $smarty->locking_timeout;
453 $cached->is_locked =
true;
454 $key =
'LOCK#' . $cached->filepath;
455 $this->
write(array($key => time()), $smarty->locking_timeout);
468 $cached->is_locked =
false;
469 $key =
'LOCK#' . $cached->filepath;
470 $this->
delete(array($key));
480 abstract protected function read(array $keys);
490 abstract protected function write(array $keys, $expire = null);
499 abstract protected function delete(array $keys);