Browse Source

refactored cache dependency methods.

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
b06d7a8675
  1. 28
      framework/yii/caching/Cache.php
  2. 15
      framework/yii/caching/ChainedDependency.php
  3. 5
      framework/yii/caching/DbDependency.php
  4. 18
      framework/yii/caching/Dependency.php
  5. 3
      framework/yii/caching/ExpressionDependency.php
  6. 3
      framework/yii/caching/FileDependency.php

28
framework/yii/caching/Cache.php

@ -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 * 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]]. * 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 * @param mixed $key the key to be normalized
* @return string the generated cache key * @return string the generated cache key
*/ */
public function buildKey($key) protected function buildKey($key)
{ {
if (is_string($key)) { if (is_string($key)) {
$key = ctype_alnum($key) && StringHelper::strlen($key) <= 32 ? $key : md5($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. * 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, * @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. * or the dependency associated with the cached data has changed.
*/ */
@ -131,7 +126,7 @@ abstract class Cache extends Component implements \ArrayAccess
} else { } else {
$value = call_user_func($this->serializer[1], $value); $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]; return $value[0];
} else { } else {
return false; return false;
@ -165,7 +160,7 @@ abstract class Cache extends Component implements \ArrayAccess
$value = $this->serializer === null ? unserialize($values[$newKey]) $value = $this->serializer === null ? unserialize($values[$newKey])
: call_user_func($this->serializer[1], $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]; $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 * If the cache already contains such a key, the existing value and
* expiration time will be replaced with the new ones, respectively. * 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 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 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, * @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) public function set($key, $value, $expire = 0, $dependency = null)
{ {
if ($dependency !== null && $this->serializer !== false) { if ($dependency !== null && $this->serializer !== false) {
$dependency->evaluateDependency(); $dependency->evaluateDependency($this);
} }
if ($this->serializer === null) { if ($this->serializer === null) {
$value = serialize(array($value, $dependency)); $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. * 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. * 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 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 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, * @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) public function add($key, $value, $expire = 0, $dependency = null)
{ {
if ($dependency !== null && $this->serializer !== false) { if ($dependency !== null && $this->serializer !== false) {
$dependency->evaluateDependency(); $dependency->evaluateDependency($this);
} }
if ($this->serializer === null) { if ($this->serializer === null) {
$value = serialize(array($value, $dependency)); $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 * 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 * @return boolean if no error happens during deletion
*/ */
public function delete($key) public function delete($key)

15
framework/yii/caching/ChainedDependency.php

@ -48,20 +48,22 @@ class ChainedDependency extends Dependency
/** /**
* 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
*/ */
public function evaluateDependency() public function evaluateDependency($cache)
{ {
foreach ($this->dependencies as $dependency) { foreach ($this->dependencies as $dependency) {
$dependency->evaluateDependency(); $dependency->evaluateDependency($cache);
} }
} }
/** /**
* 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
* @return mixed the data needed to determine if dependency has been changed. * @return mixed the data needed to determine if dependency has been changed.
*/ */
protected function generateDependencyData() protected function generateDependencyData($cache)
{ {
return null; return null;
} }
@ -70,14 +72,15 @@ class ChainedDependency extends Dependency
* Performs the actual dependency checking. * Performs the actual dependency checking.
* This method returns true if any of the dependency objects * This method returns true if any of the dependency objects
* reports a dependency change. * 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. * @return boolean whether the dependency is changed or not.
*/ */
public function getHasChanged() public function getHasChanged($cache)
{ {
foreach ($this->dependencies as $dependency) { foreach ($this->dependencies as $dependency) {
if ($this->dependOnAll && $dependency->getHasChanged()) { if ($this->dependOnAll && $dependency->getHasChanged($cache)) {
return true; return true;
} elseif (!$this->dependOnAll && !$dependency->getHasChanged()) { } elseif (!$this->dependOnAll && !$dependency->getHasChanged($cache)) {
return false; return false;
} }
} }

5
framework/yii/caching/DbDependency.php

@ -52,10 +52,11 @@ class DbDependency extends Dependency
/** /**
* 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 value of the global state. * 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. * @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); $db = Yii::$app->getComponent($this->db);
if (!$db instanceof Connection) { if (!$db instanceof Connection) {

18
framework/yii/caching/Dependency.php

@ -46,35 +46,38 @@ abstract class Dependency extends \yii\base\Object
/** /**
* Evaluates the dependency by generating and saving the data related with dependency. * Evaluates the dependency by generating and saving the data related with dependency.
* This method is invoked by cache before writing data into it. * 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) { if (!$this->reusable) {
$this->data = $this->generateDependencyData(); $this->data = $this->generateDependencyData($cache);
} else { } else {
if ($this->_hash === null) { if ($this->_hash === null) {
$this->_hash = sha1(serialize($this)); $this->_hash = sha1(serialize($this));
} }
if (!array_key_exists($this->_hash, self::$_reusableData)) { 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]; $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. * @return boolean whether the dependency has changed.
*/ */
public function getHasChanged() public function getHasChanged($cache)
{ {
if (!$this->reusable) { if (!$this->reusable) {
return $this->generateDependencyData() !== $this->data; return $this->generateDependencyData($cache) !== $this->data;
} else { } else {
if ($this->_hash === null) { if ($this->_hash === null) {
$this->_hash = sha1(serialize($this)); $this->_hash = sha1(serialize($this));
} }
if (!array_key_exists($this->_hash, self::$_reusableData)) { 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; 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. * 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
* @return mixed the data needed to determine if dependency has been changed. * @return mixed the data needed to determine if dependency has been changed.
*/ */
abstract protected function generateDependencyData(); abstract protected function generateDependencyData($cache);
} }

3
framework/yii/caching/ExpressionDependency.php

@ -50,9 +50,10 @@ class ExpressionDependency extends Dependency
/** /**
* 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.
* @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.
*/ */
protected function generateDependencyData() protected function generateDependencyData($cache)
{ {
return eval("return {$this->expression};"); return eval("return {$this->expression};");
} }

3
framework/yii/caching/FileDependency.php

@ -38,9 +38,10 @@ class FileDependency extends Dependency
/** /**
* 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
* @return mixed the data needed to determine if dependency has been changed. * @return mixed the data needed to determine if dependency has been changed.
*/ */
protected function generateDependencyData() protected function generateDependencyData($cache)
{ {
return @filemtime($this->fileName); return @filemtime($this->fileName);
} }

Loading…
Cancel
Save