mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-12 03:30:12 +08:00
Fixes #1162: removed constructors of cache dependency classes so that they can be easily created from configuration.
This commit is contained in:
@@ -23,7 +23,7 @@ class ChainedDependency extends Dependency
|
|||||||
* @var Dependency[] list of dependencies that this dependency is composed of.
|
* @var Dependency[] list of dependencies that this dependency is composed of.
|
||||||
* Each array element must be a dependency object.
|
* Each array element must be a dependency object.
|
||||||
*/
|
*/
|
||||||
public $dependencies;
|
public $dependencies = [];
|
||||||
/**
|
/**
|
||||||
* @var boolean whether this dependency is depending on every dependency in [[dependencies]].
|
* @var boolean whether this dependency is depending on every dependency in [[dependencies]].
|
||||||
* Defaults to true, meaning if any of the dependencies has changed, this dependency is considered changed.
|
* Defaults to true, meaning if any of the dependencies has changed, this dependency is considered changed.
|
||||||
@@ -32,18 +32,6 @@ class ChainedDependency extends Dependency
|
|||||||
*/
|
*/
|
||||||
public $dependOnAll = true;
|
public $dependOnAll = true;
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor.
|
|
||||||
* @param Dependency[] $dependencies list of dependencies that this dependency is composed of.
|
|
||||||
* Each array element should be a dependency object.
|
|
||||||
* @param array $config name-value pairs that will be used to initialize the object properties
|
|
||||||
*/
|
|
||||||
public function __construct($dependencies = [], $config = [])
|
|
||||||
{
|
|
||||||
$this->dependencies = $dependencies;
|
|
||||||
parent::__construct($config);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Evaluates the dependency by generating and saving the data related with dependency.
|
* Evaluates the dependency by generating and saving the data related with dependency.
|
||||||
* @param Cache $cache the cache component that is currently evaluating this dependency
|
* @param Cache $cache the cache component that is currently evaluating this dependency
|
||||||
|
|||||||
@@ -34,20 +34,7 @@ class DbDependency extends Dependency
|
|||||||
/**
|
/**
|
||||||
* @var array the parameters (name => value) to be bound to the SQL statement specified by [[sql]].
|
* @var array the parameters (name => value) to be bound to the SQL statement specified by [[sql]].
|
||||||
*/
|
*/
|
||||||
public $params;
|
public $params = [];
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor.
|
|
||||||
* @param string $sql the SQL query whose result is used to determine if the dependency has been changed.
|
|
||||||
* @param array $params the parameters (name => value) to be bound to the SQL statement specified by [[sql]].
|
|
||||||
* @param array $config name-value pairs that will be used to initialize the object properties
|
|
||||||
*/
|
|
||||||
public function __construct($sql, $params = [], $config = [])
|
|
||||||
{
|
|
||||||
$this->sql = $sql;
|
|
||||||
$this->params = $params;
|
|
||||||
parent::__construct($config);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates the data needed to determine if dependency has been changed.
|
* Generates the data needed to determine if dependency has been changed.
|
||||||
@@ -62,6 +49,9 @@ class DbDependency extends Dependency
|
|||||||
if (!$db instanceof Connection) {
|
if (!$db instanceof Connection) {
|
||||||
throw new InvalidConfigException("DbDependency::db must be the application component ID of a DB connection.");
|
throw new InvalidConfigException("DbDependency::db must be the application component ID of a DB connection.");
|
||||||
}
|
}
|
||||||
|
if ($this->sql === null) {
|
||||||
|
throw new InvalidConfigException("DbDependency::sql must be set.");
|
||||||
|
}
|
||||||
|
|
||||||
if ($db->enableQueryCache) {
|
if ($db->enableQueryCache) {
|
||||||
// temporarily disable and re-enable query caching
|
// temporarily disable and re-enable query caching
|
||||||
|
|||||||
@@ -27,26 +27,13 @@ class ExpressionDependency extends Dependency
|
|||||||
* A PHP expression can be any PHP code that evaluates to a value. To learn more about what an expression is,
|
* A PHP expression can be any PHP code that evaluates to a value. To learn more about what an expression is,
|
||||||
* please refer to the [php manual](http://www.php.net/manual/en/language.expressions.php).
|
* please refer to the [php manual](http://www.php.net/manual/en/language.expressions.php).
|
||||||
*/
|
*/
|
||||||
public $expression;
|
public $expression = 'true';
|
||||||
/**
|
/**
|
||||||
* @var mixed custom parameters associated with this dependency. You may get the value
|
* @var mixed custom parameters associated with this dependency. You may get the value
|
||||||
* of this property in [[expression]] using `$this->params`.
|
* of this property in [[expression]] using `$this->params`.
|
||||||
*/
|
*/
|
||||||
public $params;
|
public $params;
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor.
|
|
||||||
* @param string $expression the PHP expression whose result is used to determine the dependency.
|
|
||||||
* @param mixed $params the custom parameters associated with this dependency
|
|
||||||
* @param array $config name-value pairs that will be used to initialize the object properties
|
|
||||||
*/
|
|
||||||
public function __construct($expression = 'true', $params = null, $config = [])
|
|
||||||
{
|
|
||||||
$this->expression = $expression;
|
|
||||||
$this->params = $params;
|
|
||||||
parent::__construct($config);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates the data needed to determine if dependency has been changed.
|
* Generates the data needed to determine if dependency has been changed.
|
||||||
* This method returns the result of the PHP expression.
|
* This method returns the result of the PHP expression.
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
namespace yii\caching;
|
namespace yii\caching;
|
||||||
|
use yii\base\InvalidConfigException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FileDependency represents a dependency based on a file's last modification time.
|
* FileDependency represents a dependency based on a file's last modification time.
|
||||||
@@ -24,25 +25,18 @@ class FileDependency extends Dependency
|
|||||||
*/
|
*/
|
||||||
public $fileName;
|
public $fileName;
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor.
|
|
||||||
* @param string $fileName name of the file whose change is to be checked.
|
|
||||||
* @param array $config name-value pairs that will be used to initialize the object properties
|
|
||||||
*/
|
|
||||||
public function __construct($fileName = null, $config = [])
|
|
||||||
{
|
|
||||||
$this->fileName = $fileName;
|
|
||||||
parent::__construct($config);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates the data needed to determine if dependency has been changed.
|
* Generates the data needed to determine if dependency has been changed.
|
||||||
* This method returns the file's last modification time.
|
* This method returns the file's last modification time.
|
||||||
* @param Cache $cache the cache component that is currently evaluating this dependency
|
* @param Cache $cache the cache component that is currently evaluating this dependency
|
||||||
* @return mixed the data needed to determine if dependency has been changed.
|
* @return mixed the data needed to determine if dependency has been changed.
|
||||||
|
* @throws InvalidConfigException if [[fileName]] is not set
|
||||||
*/
|
*/
|
||||||
protected function generateDependencyData($cache)
|
protected function generateDependencyData($cache)
|
||||||
{
|
{
|
||||||
|
if ($this->fileName === null) {
|
||||||
|
throw new InvalidConfigException('FileDependency::fileName must be set');
|
||||||
|
}
|
||||||
return @filemtime($this->fileName);
|
return @filemtime($this->fileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
namespace yii\caching;
|
namespace yii\caching;
|
||||||
|
use yii\base\InvalidConfigException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GroupDependency marks a cached data item with a group name.
|
* GroupDependency marks a cached data item with a group name.
|
||||||
@@ -19,29 +20,22 @@ namespace yii\caching;
|
|||||||
class GroupDependency extends Dependency
|
class GroupDependency extends Dependency
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var string the group name
|
* @var string the group name. This property must be set.
|
||||||
*/
|
*/
|
||||||
public $group;
|
public $group;
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor.
|
|
||||||
* @param string $group the group name
|
|
||||||
* @param array $config name-value pairs that will be used to initialize the object properties
|
|
||||||
*/
|
|
||||||
public function __construct($group, $config = [])
|
|
||||||
{
|
|
||||||
$this->group = $group;
|
|
||||||
parent::__construct($config);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates the data needed to determine if dependency has been changed.
|
* Generates the data needed to determine if dependency has been changed.
|
||||||
* This method does nothing in this class.
|
* This method does nothing in this class.
|
||||||
* @param Cache $cache the cache component that is currently evaluating this dependency
|
* @param Cache $cache the cache component that is currently evaluating this dependency
|
||||||
* @return mixed the data needed to determine if dependency has been changed.
|
* @return mixed the data needed to determine if dependency has been changed.
|
||||||
|
* @throws InvalidConfigException if [[group]] is not set.
|
||||||
*/
|
*/
|
||||||
protected function generateDependencyData($cache)
|
protected function generateDependencyData($cache)
|
||||||
{
|
{
|
||||||
|
if ($this->group === null) {
|
||||||
|
throw new InvalidConfigException('GroupDependency::group must be set');
|
||||||
|
}
|
||||||
$version = $cache->get([__CLASS__, $this->group]);
|
$version = $cache->get([__CLASS__, $this->group]);
|
||||||
if ($version === false) {
|
if ($version === false) {
|
||||||
$version = $this->invalidate($cache, $this->group);
|
$version = $this->invalidate($cache, $this->group);
|
||||||
@@ -53,9 +47,13 @@ class GroupDependency extends Dependency
|
|||||||
* Performs the actual dependency checking.
|
* Performs the actual dependency checking.
|
||||||
* @param Cache $cache the cache component that is currently evaluating this dependency
|
* @param Cache $cache the cache component that is currently evaluating this dependency
|
||||||
* @return boolean whether the dependency is changed or not.
|
* @return boolean whether the dependency is changed or not.
|
||||||
|
* @throws InvalidConfigException if [[group]] is not set.
|
||||||
*/
|
*/
|
||||||
public function getHasChanged($cache)
|
public function getHasChanged($cache)
|
||||||
{
|
{
|
||||||
|
if ($this->group === null) {
|
||||||
|
throw new InvalidConfigException('GroupDependency::group must be set');
|
||||||
|
}
|
||||||
$version = $cache->get([__CLASS__, $this->group]);
|
$version = $cache->get([__CLASS__, $this->group]);
|
||||||
return $version === false || $version !== $this->data;
|
return $version === false || $version !== $this->data;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user