- 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)
$start = microtime(true);
$hadLock = null;
while ($r = $this->hasLock($smarty, $cached)) {
while ($this->hasLock($smarty, $cached)) {
$hadLock = true;
if (microtime(true) - $start > $smarty->locking_timeout) {
// 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;
$_compile_id = isset($cached->compile_id) ? preg_replace('![^\w\|]+!', '_', $cached->compile_id) : null;
$cached->filepath = sha1($cached->source->filepath . $_cache_id . $_compile_id);
$path = $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);
}
@@ -266,15 +269,15 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
*/
public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
{
$id = $cached->filepath;
$id = $cached->lock_id;
$name = $cached->source->name . '.lock';
$mtime = $this->fetchTimestamp($id, $name, null, null);
if ($mtime === null) {
$this->fetch($id, $name, null, null, $content, $mtime);
}
return $mtime && time() - $mtime < $smarty->locking_timeout;
$stat = $mtime && ($t = 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)
{
$cached->is_locked = true;
$id = $cached->filepath;
$id = $cached->lock_id;
$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)
{
$cached->is_locked = false;
$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)
{
if ($this->valid !== null) {
return $this->valid;
if ($this->valid === true) {
return true;
}
$force = $_template->smarty->force_compile || $_template->smarty->force_cache;
if (!$lock && ($force || !$_template->caching)) {
return $this->valid = false;
}
while (true) {
while (true) {
$this->handler->populate($this, $_template);
if ($this->timestamp === false || $_template->smarty->force_compile || $_template->smarty->force_cache) {
$this->valid = false;
} else {
$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)) {
// lifetime expired
$this->valid = false;
@@ -167,9 +169,8 @@ class Smarty_Template_Cached
}
if (!$this->handler->locked($_template->smarty, $this)) {
$this->handler->acquireLock($_template->smarty, $this);
break;
break 2;
}
$this->handler->populate($this, $_template);
}
if ($this->valid) {
if (!$_template->smarty->cache_locking || $this->handler->locked($_template->smarty, $this) === null) {
@@ -177,7 +178,11 @@ class Smarty_Template_Cached
if ($_template->smarty->debugging) {
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) {
Smarty_Internal_Debug::end_cache($_template);
}
@@ -185,23 +190,21 @@ class Smarty_Template_Cached
continue;
}
} else {
if ($_template->smarty->cache_locking && !$lock) {
$this->handler->releaseLock($_template->smarty, $this);
}
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']))) {
$this->valid = false;
}
if (!$this->valid && $_template->smarty->cache_locking && $lock) {
if (!$this->valid && $_template->smarty->cache_locking) {
$this->handler->acquireLock($_template->smarty, $this);
}
if ($_template->smarty->cache_locking && !$lock) {
$this->handler->releaseLock($_template->smarty, $this);
}
return $this->valid;
} else {
return $this->valid;
}
}
return $this->valid =false;
}
/**
* Process cached template
@@ -252,12 +255,18 @@ class Smarty_Template_Cached
$this->timestamp = time();
$this->exists = true;
$this->valid = true;
$this->processed = false;
if ($_template->smarty->cache_locking) {
$this->handler->releaseLock($_template->smarty, $this);
}
return true;
}
$this->content = null;
$this->timestamp = false;
$this->exists = false;
$this->valid = false;
$this->processed = false;
}
return false;