Commit 4674f5b7 by Qiang Xue

Fixes #3052: Fixed the issue that cache dependency data is not reused when `reusable` is set true

parent 3349af1a
...@@ -64,6 +64,7 @@ Yii Framework 2 Change Log ...@@ -64,6 +64,7 @@ Yii Framework 2 Change Log
- Bug #2834: When overriding i18n translation sources from config using `app*` or `yii*` default `app` and `yii` sources were not removed (samdark) - Bug #2834: When overriding i18n translation sources from config using `app*` or `yii*` default `app` and `yii` sources were not removed (samdark)
- Bug #2848: Individual queries should be enclosed within parenthesis in a UNION query (qiangxue) - Bug #2848: Individual queries should be enclosed within parenthesis in a UNION query (qiangxue)
- Bug #2862: Using `DbCache` while enabling schema caching may cause infinite loops (qiangxue) - Bug #2862: Using `DbCache` while enabling schema caching may cause infinite loops (qiangxue)
- Bug #3052: Fixed the issue that cache dependency data is not reused when `reusable` is set true (qiangxue)
- Bug: Fixed `Call to a member function registerAssetFiles() on a non-object` in case of wrong `sourcePath` for an asset bundle (samdark) - Bug: Fixed `Call to a member function registerAssetFiles() on a non-object` in case of wrong `sourcePath` for an asset bundle (samdark)
- Bug: Fixed incorrect event name for `yii\jui\Spinner` (samdark) - Bug: Fixed incorrect event name for `yii\jui\Spinner` (samdark)
- Bug: Json::encode() did not handle objects that implement JsonSerializable interface correctly (cebe) - Bug: Json::encode() did not handle objects that implement JsonSerializable interface correctly (cebe)
......
...@@ -35,10 +35,6 @@ abstract class Dependency extends \yii\base\Object ...@@ -35,10 +35,6 @@ abstract class Dependency extends \yii\base\Object
* @var array static storage of cached data for reusable dependencies. * @var array static storage of cached data for reusable dependencies.
*/ */
private static $_reusableData = []; private static $_reusableData = [];
/**
* @var string a unique hash value for this cache dependency.
*/
private $_hash;
/** /**
* Evaluates the dependency by generating and saving the data related with dependency. * Evaluates the dependency by generating and saving the data related with dependency.
...@@ -47,16 +43,14 @@ abstract class Dependency extends \yii\base\Object ...@@ -47,16 +43,14 @@ abstract class Dependency extends \yii\base\Object
*/ */
public function evaluateDependency($cache) public function evaluateDependency($cache)
{ {
if (!$this->reusable) { if ($this->reusable) {
$this->data = $this->generateDependencyData($cache); $hash = $this->generateReusableHash();
} else { if (!array_key_exists($hash, self::$_reusableData)) {
if ($this->_hash === null) { self::$_reusableData[$hash] = $this->generateDependencyData($cache);
$this->_hash = sha1(serialize($this));
} }
if (!array_key_exists($this->_hash, self::$_reusableData)) { $this->data = self::$_reusableData[$hash];
self::$_reusableData[$this->_hash] = $this->generateDependencyData($cache); } else {
} $this->data = $this->generateDependencyData($cache);
$this->data = self::$_reusableData[$this->_hash];
} }
} }
...@@ -67,18 +61,16 @@ abstract class Dependency extends \yii\base\Object ...@@ -67,18 +61,16 @@ abstract class Dependency extends \yii\base\Object
*/ */
public function getHasChanged($cache) public function getHasChanged($cache)
{ {
if (!$this->reusable) { if ($this->reusable) {
return $this->generateDependencyData($cache) !== $this->data; $hash = $this->generateReusableHash();
} else { if (!array_key_exists($hash, self::$_reusableData)) {
if ($this->_hash === null) { self::$_reusableData[$hash] = $this->generateDependencyData($cache);
$this->_hash = sha1(serialize($this));
} }
if (!array_key_exists($this->_hash, self::$_reusableData)) { $data = self::$_reusableData[$hash];
self::$_reusableData[$this->_hash] = $this->generateDependencyData($cache); } else {
} $data = $this->generateDependencyData($cache);
return self::$_reusableData[$this->_hash] !== $this->data;
} }
return $data !== $this->data;
} }
/** /**
...@@ -90,6 +82,20 @@ abstract class Dependency extends \yii\base\Object ...@@ -90,6 +82,20 @@ abstract class Dependency extends \yii\base\Object
} }
/** /**
* Generates a unique hash that can be used for retrieving reusable dependency data.
* @return string a unique hash value for this cache dependency.
* @see reusable
*/
protected function generateReusableHash()
{
$data = $this->data;
$this->data = null; // https://github.com/yiisoft/yii2/issues/3052
$key = sha1(serialize($this));
$this->data = $data;
return $key;
}
/**
* Generates the data needed to determine if dependency has been changed. * Generates the data needed to determine if dependency has been changed.
* Derived classes should override this method to generate the actual dependency data. * Derived classes should override this method to generate the actual dependency data.
* @param Cache $cache the cache component that is currently evaluating this dependency * @param Cache $cache the cache component that is currently evaluating this dependency
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment