- uodate and fixes at cache locking

This commit is contained in:
Uwe Tews
2015-05-08 07:27:45 +02:00
parent d7b0b57420
commit af4a923da5
3 changed files with 38 additions and 28 deletions

View File

@@ -116,7 +116,7 @@ abstract class Smarty_CacheResource
// theoretically locking_timeout should be checked against time_limit (max_execution_time) // theoretically locking_timeout should be checked against time_limit (max_execution_time)
$start = microtime(true); $start = microtime(true);
$hadLock = null; $hadLock = null;
while ($r = $this->hasLock($smarty, $cached)) { while ($this->hasLock($smarty, $cached)) {
$hadLock = true; $hadLock = true;
if (microtime(true) - $start > $smarty->locking_timeout) { if (microtime(true) - $start > $smarty->locking_timeout) {
// abort waiting for lock release // abort waiting for lock release

View File

@@ -84,8 +84,11 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
{ {
$_cache_id = isset($cached->cache_id) ? preg_replace('![^\w\|]+!', '_', $cached->cache_id) : null; $_cache_id = isset($cached->cache_id) ? preg_replace('![^\w\|]+!', '_', $cached->cache_id) : null;
$_compile_id = isset($cached->compile_id) ? preg_replace('![^\w\|]+!', '_', $cached->compile_id) : null; $_compile_id = isset($cached->compile_id) ? preg_replace('![^\w\|]+!', '_', $cached->compile_id) : null;
$path = $cached->source->filepath . $_cache_id . $_compile_id;
$cached->filepath = sha1($cached->source->filepath . $_cache_id . $_compile_id); $cached->filepath = sha1($path);
if ($_template->smarty->cache_locking) {
$cached->lock_id = sha1('lock.' . $path);
}
$this->populateTimestamp($cached); $this->populateTimestamp($cached);
} }
@@ -266,15 +269,15 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
*/ */
public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached) public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
{ {
$id = $cached->filepath; $id = $cached->lock_id;
$name = $cached->source->name . '.lock'; $name = $cached->source->name . '.lock';
$mtime = $this->fetchTimestamp($id, $name, null, null); $mtime = $this->fetchTimestamp($id, $name, null, null);
if ($mtime === null) { if ($mtime === null) {
$this->fetch($id, $name, null, null, $content, $mtime); $this->fetch($id, $name, null, null, $content, $mtime);
} }
$stat = $mtime && ($t = time()) - $mtime < $smarty->locking_timeout;
return $mtime && time() - $mtime < $smarty->locking_timeout; return $stat ? $mtime : $stat;
} }
/** /**
@@ -288,10 +291,9 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached) public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
{ {
$cached->is_locked = true; $cached->is_locked = true;
$id = $cached->lock_id;
$id = $cached->filepath;
$name = $cached->source->name . '.lock'; $name = $cached->source->name . '.lock';
$this->save($id, $name, null, null, $smarty->locking_timeout, ''); $this->save($id, $name, $cached->cache_id, $cached->compile_id, $smarty->locking_timeout, '');
} }
/** /**
@@ -305,8 +307,7 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached) public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
{ {
$cached->is_locked = false; $cached->is_locked = false;
$name = $cached->source->name . '.lock'; $name = $cached->source->name . '.lock';
$this->delete($name, null, null, null); $this->delete($name, $cached->cache_id, $cached->compile_id, null);
} }
} }

View File

@@ -145,19 +145,21 @@ class Smarty_Template_Cached
*/ */
public function isCached(Smarty_Internal_Template $_template, $lock = false) public function isCached(Smarty_Internal_Template $_template, $lock = false)
{ {
if ($this->valid !== null) { if ($this->valid === true) {
return $this->valid; return true;
}
$force = $_template->smarty->force_compile || $_template->smarty->force_cache;
if (!$lock && ($force || !$_template->caching)) {
return $this->valid = false;
} }
while (true) { while (true) {
while (true) { while (true) {
$this->handler->populate($this, $_template);
if ($this->timestamp === false || $_template->smarty->force_compile || $_template->smarty->force_cache) { if ($this->timestamp === false || $_template->smarty->force_compile || $_template->smarty->force_cache) {
$this->valid = false; $this->valid = false;
} else { } else {
$this->valid = true; $this->valid = true;
} }
if ($this->valid && $_template->source->timestamp > $this->timestamp) {
$this->valid = false;
}
if ($this->valid && $_template->caching == Smarty::CACHING_LIFETIME_CURRENT && $_template->cache_lifetime >= 0 && time() > ($this->timestamp + $_template->cache_lifetime)) { if ($this->valid && $_template->caching == Smarty::CACHING_LIFETIME_CURRENT && $_template->cache_lifetime >= 0 && time() > ($this->timestamp + $_template->cache_lifetime)) {
// lifetime expired // lifetime expired
$this->valid = false; $this->valid = false;
@@ -167,9 +169,8 @@ class Smarty_Template_Cached
} }
if (!$this->handler->locked($_template->smarty, $this)) { if (!$this->handler->locked($_template->smarty, $this)) {
$this->handler->acquireLock($_template->smarty, $this); $this->handler->acquireLock($_template->smarty, $this);
break; break 2;
} }
$this->handler->populate($this, $_template);
} }
if ($this->valid) { if ($this->valid) {
if (!$_template->smarty->cache_locking || $this->handler->locked($_template->smarty, $this) === null) { if (!$_template->smarty->cache_locking || $this->handler->locked($_template->smarty, $this) === null) {
@@ -177,7 +178,11 @@ class Smarty_Template_Cached
if ($_template->smarty->debugging) { if ($_template->smarty->debugging) {
Smarty_Internal_Debug::start_cache($_template); Smarty_Internal_Debug::start_cache($_template);
} }
$this->process($_template); if ($this->handler->process($_template, $this) === false) {
$this->valid = false;
} else {
$this->processed = true;
}
if ($_template->smarty->debugging) { if ($_template->smarty->debugging) {
Smarty_Internal_Debug::end_cache($_template); Smarty_Internal_Debug::end_cache($_template);
} }
@@ -185,23 +190,21 @@ class Smarty_Template_Cached
continue; continue;
} }
} else { } else {
if ($_template->smarty->cache_locking && !$lock) {
$this->handler->releaseLock($_template->smarty, $this);
}
return $this->valid; return $this->valid;
} }
if ($this->valid && $_template->caching === Smarty::CACHING_LIFETIME_SAVED && $_template->properties['cache_lifetime'] >= 0 && (time() > ($_template->cached->timestamp + $_template->properties['cache_lifetime']))) { if ($this->valid && $_template->caching === Smarty::CACHING_LIFETIME_SAVED && $_template->properties['cache_lifetime'] >= 0 && (time() > ($_template->cached->timestamp + $_template->properties['cache_lifetime']))) {
$this->valid = false; $this->valid = false;
} }
if (!$this->valid && $_template->smarty->cache_locking && $lock) { if (!$this->valid && $_template->smarty->cache_locking) {
$this->handler->acquireLock($_template->smarty, $this); $this->handler->acquireLock($_template->smarty, $this);
return $this->valid;
} else {
return $this->valid;
} }
if ($_template->smarty->cache_locking && !$lock) {
$this->handler->releaseLock($_template->smarty, $this);
}
return $this->valid;
} }
} return $this->valid =false;
}
/** /**
* Process cached template * Process cached template
@@ -252,12 +255,18 @@ class Smarty_Template_Cached
$this->timestamp = time(); $this->timestamp = time();
$this->exists = true; $this->exists = true;
$this->valid = true; $this->valid = true;
$this->processed = false;
if ($_template->smarty->cache_locking) { if ($_template->smarty->cache_locking) {
$this->handler->releaseLock($_template->smarty, $this); $this->handler->releaseLock($_template->smarty, $this);
} }
return true; return true;
} }
$this->content = null;
$this->timestamp = false;
$this->exists = false;
$this->valid = false;
$this->processed = false;
} }
return false; return false;