diff --git a/change_log.txt b/change_log.txt index ab4b7f53..82a5ab9a 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,6 +1,7 @@ ===== trunk ===== 14.10.2011 - bugfix unique_resource did not properly apply to compiled resources (Forum Topic 20128) +- add locking to custom resources (Forum Post 75252) 13.10.2011 - add caching for config files in Smarty_Resource diff --git a/libs/sysplugins/smarty_cacheresource.php b/libs/sysplugins/smarty_cacheresource.php index 23d3ce02..28df37a8 100644 --- a/libs/sysplugins/smarty_cacheresource.php +++ b/libs/sysplugins/smarty_cacheresource.php @@ -150,6 +150,7 @@ abstract class Smarty_CacheResource { } // try the instance cache if (isset(self::$resources[$type])) { + // FIXME: rodneyrehm need to validate if cache resource may be used in given $smarty. return self::$resources[$type]; } // try registered resource diff --git a/libs/sysplugins/smarty_cacheresource_custom.php b/libs/sysplugins/smarty_cacheresource_custom.php index beec39f3..d7513822 100644 --- a/libs/sysplugins/smarty_cacheresource_custom.php +++ b/libs/sysplugins/smarty_cacheresource_custom.php @@ -184,6 +184,55 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource { $this->cache = array(); return $this->delete($resource_name, $cache_id, $compile_id, $exp_time); } + + /** + * Check is cache is locked for this template + * + * @param Smarty $smarty Smarty object + * @param Smarty_Template_Cached $cached cached object + * @return booelan true or false if cache is locked + */ + public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached) + { + $id = $cached->filepath; + $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; + } + /** + * Lock cache for this template + * + * @param Smarty $smarty Smarty object + * @param Smarty_Template_Cached $cached cached object + */ + public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached) + { + $cached->is_locked = true; + + $id = $cached->filepath; + $name = $cached->source->name . '.lock'; + $this->save($id, $name, null, null, $smarty->locking_timeout, ''); + } + + /** + * Unlock cache for this template + * + * @param Smarty $smarty Smarty object + * @param Smarty_Template_Cached $cached cached object + */ + public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached) + { + $cached->is_locked = false; + + $id = $cached->filepath; + $name = $cached->source->name . '.lock'; + $this->delete($name, null, null, null); + } } ?> \ No newline at end of file diff --git a/libs/sysplugins/smarty_resource.php b/libs/sysplugins/smarty_resource.php index bc4e18bf..f2e3c1b2 100644 --- a/libs/sysplugins/smarty_resource.php +++ b/libs/sysplugins/smarty_resource.php @@ -339,6 +339,7 @@ abstract class Smarty_Resource { { // try the instance cache if (isset(self::$resources[$resource_type])) { + // FIXME: rodneyrehm need to validate if resource may be used in given $smarty. return self::$resources[$resource_type]; }