From ebcc936ab1162b05e168061df64bb548b0b920a0 Mon Sep 17 00:00:00 2001 From: resurtm Date: Wed, 22 May 2013 15:10:14 +0600 Subject: [PATCH 1/2] Fixes #145. Reusable cache dependencies. --- framework/yii/caching/Dependency.php | 48 ++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/framework/yii/caching/Dependency.php b/framework/yii/caching/Dependency.php index d1428fc..f5f2af5 100644 --- a/framework/yii/caching/Dependency.php +++ b/framework/yii/caching/Dependency.php @@ -25,6 +25,22 @@ abstract class Dependency extends \yii\base\Object * latest dependency data. */ public $data; + /** + * @var boolean whether this dependency is reusable or not. True value means that dependent + * data for this cache dependency will only be generated once per request. This allows you + * to use the same cache dependency for multiple separate cache calls while generating the same + * page without an overhead of re-evaluating dependency data each time. Defaults to false. + */ + public $reuseData = false; + + /** + * @var array static storage of cached data for reusable dependencies. + */ + private static $_reusableData = array(); + /** + * @var string a unique hash value for this cache dependency. + */ + private $_hash; /** * Evaluates the dependency by generating and saving the data related with dependency. @@ -32,7 +48,17 @@ abstract class Dependency extends \yii\base\Object */ public function evaluateDependency() { - $this->data = $this->generateDependencyData(); + if (!$this->reuseData) { + $this->data = $this->generateDependencyData(); + } else { + if ($this->_hash === null) { + $this->_hash = sha1(serialize($this)); + } + if (!array_key_exists($this->_hash, self::$_reusableData)) { + self::$_reusableData[$this->_hash] = $this->generateDependencyData(); + } + $this->data = self::$_reusableData[$this->_hash]; + } } /** @@ -40,7 +66,25 @@ abstract class Dependency extends \yii\base\Object */ public function getHasChanged() { - return $this->generateDependencyData() !== $this->data; + if (!$this->reuseData) { + return $this->generateDependencyData() !== $this->data; + } else { + if ($this->_hash === null) { + $this->_hash = sha1(serialize($this)); + } + if (!array_key_exists($this->_hash, self::$_reusableData)) { + self::$_reusableData[$this->_hash] = $this->generateDependencyData(); + } + return self::$_reusableData[$this->_hash] !== $this->_data; + } + } + + /** + * Resets all cached data for reusable dependencies. + */ + public static function resetReusableData() + { + self::$_reusableData = array(); } /** From 54610f179f91f4e9fe80e1b534bc905ad134334f Mon Sep 17 00:00:00 2001 From: resurtm Date: Wed, 22 May 2013 22:56:17 +0600 Subject: [PATCH 2/2] Better naming. Additional newline. Doctype tiny fix. --- framework/yii/caching/Dependency.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/framework/yii/caching/Dependency.php b/framework/yii/caching/Dependency.php index f5f2af5..f0587f9 100644 --- a/framework/yii/caching/Dependency.php +++ b/framework/yii/caching/Dependency.php @@ -27,11 +27,11 @@ abstract class Dependency extends \yii\base\Object public $data; /** * @var boolean whether this dependency is reusable or not. True value means that dependent - * data for this cache dependency will only be generated once per request. This allows you + * data for this cache dependency will be generated only once per request. This allows you * to use the same cache dependency for multiple separate cache calls while generating the same * page without an overhead of re-evaluating dependency data each time. Defaults to false. */ - public $reuseData = false; + public $reusable = false; /** * @var array static storage of cached data for reusable dependencies. @@ -42,13 +42,14 @@ abstract class Dependency extends \yii\base\Object */ private $_hash; + /** * Evaluates the dependency by generating and saving the data related with dependency. * This method is invoked by cache before writing data into it. */ public function evaluateDependency() { - if (!$this->reuseData) { + if (!$this->reusable) { $this->data = $this->generateDependencyData(); } else { if ($this->_hash === null) { @@ -66,7 +67,7 @@ abstract class Dependency extends \yii\base\Object */ public function getHasChanged() { - if (!$this->reuseData) { + if (!$this->reusable) { return $this->generateDependencyData() !== $this->data; } else { if ($this->_hash === null) {