Commit b06d7a86 by Qiang Xue

refactored cache dependency methods.

parent 870f6534
......@@ -95,16 +95,10 @@ abstract class Cache extends Component implements \ArrayAccess
* then the key will be returned back prefixed with [[keyPrefix]]. Otherwise, a normalized key
* is generated by serializing the given key, applying MD5 hashing, and prefixing with [[keyPrefix]].
*
* The following example builds a cache key using three parameters:
*
* ~~~
* $key = $cache->buildKey(array($className, $method, $id));
* ~~~
*
* @param mixed $key the key to be normalized
* @return string the generated cache key
*/
public function buildKey($key)
protected function buildKey($key)
{
if (is_string($key)) {
$key = ctype_alnum($key) && StringHelper::strlen($key) <= 32 ? $key : md5($key);
......@@ -116,7 +110,8 @@ abstract class Cache extends Component implements \ArrayAccess
/**
* Retrieves a value from cache with a specified key.
* @param string $key a key identifying the cached value
* @param mixed $key a key identifying the cached value. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @return mixed the value stored in cache, false if the value is not in the cache, expired,
* or the dependency associated with the cached data has changed.
*/
......@@ -131,7 +126,7 @@ abstract class Cache extends Component implements \ArrayAccess
} else {
$value = call_user_func($this->serializer[1], $value);
}
if (is_array($value) && !($value[1] instanceof Dependency && $value[1]->getHasChanged())) {
if (is_array($value) && !($value[1] instanceof Dependency && $value[1]->getHasChanged($this))) {
return $value[0];
} else {
return false;
......@@ -165,7 +160,7 @@ abstract class Cache extends Component implements \ArrayAccess
$value = $this->serializer === null ? unserialize($values[$newKey])
: call_user_func($this->serializer[1], $values[$newKey]);
if (is_array($value) && !($value[1] instanceof Dependency && $value[1]->getHasChanged())) {
if (is_array($value) && !($value[1] instanceof Dependency && $value[1]->getHasChanged($this))) {
$results[$key] = $value[0];
}
}
......@@ -179,7 +174,8 @@ abstract class Cache extends Component implements \ArrayAccess
* If the cache already contains such a key, the existing value and
* expiration time will be replaced with the new ones, respectively.
*
* @param string $key the key identifying the value to be cached
* @param mixed $key a key identifying the value to be cached value. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @param mixed $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param Dependency $dependency dependency of the cached item. If the dependency changes,
......@@ -190,7 +186,7 @@ abstract class Cache extends Component implements \ArrayAccess
public function set($key, $value, $expire = 0, $dependency = null)
{
if ($dependency !== null && $this->serializer !== false) {
$dependency->evaluateDependency();
$dependency->evaluateDependency($this);
}
if ($this->serializer === null) {
$value = serialize(array($value, $dependency));
......@@ -204,7 +200,8 @@ abstract class Cache extends Component implements \ArrayAccess
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* Nothing will be done if the cache already contains the key.
* @param string $key the key identifying the value to be cached
* @param mixed $key a key identifying the value to be cached value. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @param mixed $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param Dependency $dependency dependency of the cached item. If the dependency changes,
......@@ -215,7 +212,7 @@ abstract class Cache extends Component implements \ArrayAccess
public function add($key, $value, $expire = 0, $dependency = null)
{
if ($dependency !== null && $this->serializer !== false) {
$dependency->evaluateDependency();
$dependency->evaluateDependency($this);
}
if ($this->serializer === null) {
$value = serialize(array($value, $dependency));
......@@ -228,7 +225,8 @@ abstract class Cache extends Component implements \ArrayAccess
/**
* Deletes a value with the specified key from cache
* @param string $key the key of the value to be deleted
* @param mixed $key a key identifying the value to be deleted from cache. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @return boolean if no error happens during deletion
*/
public function delete($key)
......
......@@ -48,20 +48,22 @@ class ChainedDependency extends 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
*/
public function evaluateDependency()
public function evaluateDependency($cache)
{
foreach ($this->dependencies as $dependency) {
$dependency->evaluateDependency();
$dependency->evaluateDependency($cache);
}
}
/**
* Generates the data needed to determine if dependency has been changed.
* This method does nothing in this class.
* @param Cache $cache the cache component that is currently evaluating this dependency
* @return mixed the data needed to determine if dependency has been changed.
*/
protected function generateDependencyData()
protected function generateDependencyData($cache)
{
return null;
}
......@@ -70,14 +72,15 @@ class ChainedDependency extends Dependency
* Performs the actual dependency checking.
* This method returns true if any of the dependency objects
* reports a dependency change.
* @param Cache $cache the cache component that is currently evaluating this dependency
* @return boolean whether the dependency is changed or not.
*/
public function getHasChanged()
public function getHasChanged($cache)
{
foreach ($this->dependencies as $dependency) {
if ($this->dependOnAll && $dependency->getHasChanged()) {
if ($this->dependOnAll && $dependency->getHasChanged($cache)) {
return true;
} elseif (!$this->dependOnAll && !$dependency->getHasChanged()) {
} elseif (!$this->dependOnAll && !$dependency->getHasChanged($cache)) {
return false;
}
}
......
......@@ -52,10 +52,11 @@ class DbDependency extends Dependency
/**
* Generates the data needed to determine if dependency has been changed.
* This method returns the value of the global state.
* @throws InvalidConfigException
* @param Cache $cache the cache component that is currently evaluating this dependency
* @return mixed the data needed to determine if dependency has been changed.
* @throws InvalidConfigException if [[db]] is not a valid application component ID
*/
protected function generateDependencyData()
protected function generateDependencyData($cache)
{
$db = Yii::$app->getComponent($this->db);
if (!$db instanceof Connection) {
......
......@@ -46,35 +46,38 @@ abstract class Dependency extends \yii\base\Object
/**
* Evaluates the dependency by generating and saving the data related with dependency.
* This method is invoked by cache before writing data into it.
* @param Cache $cache the cache component that is currently evaluating this dependency
*/
public function evaluateDependency()
public function evaluateDependency($cache)
{
if (!$this->reusable) {
$this->data = $this->generateDependencyData();
$this->data = $this->generateDependencyData($cache);
} else {
if ($this->_hash === null) {
$this->_hash = sha1(serialize($this));
}
if (!array_key_exists($this->_hash, self::$_reusableData)) {
self::$_reusableData[$this->_hash] = $this->generateDependencyData();
self::$_reusableData[$this->_hash] = $this->generateDependencyData($cache);
}
$this->data = self::$_reusableData[$this->_hash];
}
}
/**
* Returns a value indicating whether the dependency has changed.
* @param Cache $cache the cache component that is currently evaluating this dependency
* @return boolean whether the dependency has changed.
*/
public function getHasChanged()
public function getHasChanged($cache)
{
if (!$this->reusable) {
return $this->generateDependencyData() !== $this->data;
return $this->generateDependencyData($cache) !== $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();
self::$_reusableData[$this->_hash] = $this->generateDependencyData($cache);
}
return self::$_reusableData[$this->_hash] !== $this->data;
}
......@@ -91,7 +94,8 @@ abstract class Dependency extends \yii\base\Object
/**
* Generates the data needed to determine if dependency has been changed.
* Derived classes should override this method to generate the actual dependency data.
* @param Cache $cache the cache component that is currently evaluating this dependency
* @return mixed the data needed to determine if dependency has been changed.
*/
abstract protected function generateDependencyData();
abstract protected function generateDependencyData($cache);
}
......@@ -50,9 +50,10 @@ class ExpressionDependency extends Dependency
/**
* Generates the data needed to determine if dependency has been changed.
* This method returns the result of the PHP expression.
* @param Cache $cache the cache component that is currently evaluating this dependency
* @return mixed the data needed to determine if dependency has been changed.
*/
protected function generateDependencyData()
protected function generateDependencyData($cache)
{
return eval("return {$this->expression};");
}
......
......@@ -38,9 +38,10 @@ class FileDependency extends Dependency
/**
* Generates the data needed to determine if dependency has been changed.
* This method returns the file's last modification time.
* @param Cache $cache the cache component that is currently evaluating this dependency
* @return mixed the data needed to determine if dependency has been changed.
*/
protected function generateDependencyData()
protected function generateDependencyData($cache)
{
return @filemtime($this->fileName);
}
......
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