@ -12,37 +12,21 @@ namespace yii\caching;
/**
* Dependency is the base class for cache dependency classes.
*
* Dependency implements the {@link ICacheDependency} interface.
* Child classes should override its {@link generateDependentData} for
* actual dependency checking.
* Child classes should override its [[generateDependencyData()]] for generating
* the actual dependency data.
*
* @property boolean $hasChanged Whether the dependency has changed.
* @property mixed $dependentData The data used to determine if dependency has been changed.
* This data is available after {@link evaluateDependency} is called.
*
* @author Qiang Xue < qiang.xue @ gmail . com >
* @since 2.0
*/
class Dependency extends \yii\base\Object
abstract class Dependency extends \yii\base\Object
{
/**
* @var boolean Whether this dependency is reusable or not.
* If set to true, dependent data for this cache dependency will only be generated once per request.
* You can then use the same cache dependency for multiple separate cache calls on the same page
* without the overhead of re-evaluating the dependency each time.
* Defaults to false;
* @since 1.1.11
* @var mixed the dependency data that is saved in cache and later is compared with the
* latest dependency data.
*/
public $reuseDependentData=false;
/**
* @var array cached data for reusable dependencies.
* @since 1.1.11
*/
private static $_reusableData=array();
private $_hash;
private $_data;
public $data;
/**
* Evaluates the dependency by generating and saving the data related with dependency.
@ -50,15 +34,7 @@ class Dependency extends \yii\base\Object
*/
public function evaluateDependency()
{
if ($this->reuseDependentData)
{
$hash=$this->getHash();
if (!isset(self::$_reusableData[$hash]['dependentData']))
self::$_reusableData[$hash]['dependentData']=$this->generateDependentData();
$this->_data=self::$_reusableData[$hash]['dependentData'];
}
else
$this->_data=$this->generateDependentData();
$this->data = $this->generateDependencyData();
}
/**
@ -66,47 +42,13 @@ class Dependency extends \yii\base\Object
*/
public function getHasChanged()
{
if ($this->reuseDependentData)
{
$hash=$this->getHash();
if (!isset(self::$_reusableData[$hash]['hasChanged']))
{
if (!isset(self::$_reusableData[$hash]['dependentData']))
self::$_reusableData[$hash]['dependentData']=$this->generateDependentData();
self::$_reusableData[$hash]['hasChanged']=self::$_reusableData[$hash]['dependentData']!=$this->_data;
}
return self::$_reusableData[$hash]['hasChanged'];
}
else
return $this->generateDependentData()!=$this->_data;
}
/**
* @return mixed the data used to determine if dependency has been changed.
* This data is available after {@link evaluateDependency} is called.
*/
public function getDependentData()
{
return $this->_data;
return $this->generateDependencyData() != $this->data;
}
/**
* Generates the data needed to determine if dependency has been changed.
* Derived classes should override this method to generate actual dependent data.
* Derived classes should override this method to generate the actual dependency data.
* @return mixed the data needed to determine if dependency has been changed.
*/
protected function generateDependentData()
{
return null;
}
/**
* Generates a unique hash that identifies this cache dependency.
* @return string the hash for this cache dependency
*/
private function getHash()
{
if($this->_hash===null)
$this->_hash=sha1(serialize($this));
return $this->_hash;
}
abstract protected function generateDependencyData();
}