Commit 11d374a8 by Qiang Xue

finished cleanup of caching components.

parent ce168fb3
...@@ -81,9 +81,9 @@ class ChainedDependency extends Dependency ...@@ -81,9 +81,9 @@ class ChainedDependency extends Dependency
*/ */
public function getHasChanged() public function getHasChanged()
{ {
foreach ($this->dependencies as $dependency) { foreach ($this->dependencies as $i => $dependency) {
if (!$dependency instanceof Dependency) { if (!$dependency instanceof Dependency) {
$dependency = \Yii::createObject($dependency); $this->dependencies[$i] = $dependency = \Yii::createObject($dependency);
} }
if ($this->dependOnAll && $dependency->getHasChanged()) { if ($this->dependOnAll && $dependency->getHasChanged()) {
return true; return true;
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
namespace yii\caching; namespace yii\caching;
use yii\base\Exception; use yii\base\InvalidConfigException;
use yii\db\Connection; use yii\db\Connection;
use yii\db\Query; use yii\db\Query;
...@@ -57,7 +57,7 @@ class DbCache extends Cache ...@@ -57,7 +57,7 @@ class DbCache extends Cache
public $cacheTableName = 'tbl_cache'; public $cacheTableName = 'tbl_cache';
/** /**
* @var integer the probability (parts per million) that garbage collection (GC) should be performed * @var integer the probability (parts per million) that garbage collection (GC) should be performed
* when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance. * when storing a piece of data in the cache. Defaults to 10, meaning 0.001% chance.
* This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all. * This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all.
**/ **/
public $gcProbability = 100; public $gcProbability = 100;
...@@ -69,7 +69,7 @@ class DbCache extends Cache ...@@ -69,7 +69,7 @@ class DbCache extends Cache
/** /**
* Returns the DB connection instance used for caching purpose. * Returns the DB connection instance used for caching purpose.
* @return Connection the DB connection instance * @return Connection the DB connection instance
* @throws Exception if [[connectionID]] does not point to a valid application component. * @throws InvalidConfigException if [[connectionID]] does not point to a valid application component.
*/ */
public function getDb() public function getDb()
{ {
...@@ -78,7 +78,7 @@ class DbCache extends Cache ...@@ -78,7 +78,7 @@ class DbCache extends Cache
if ($db instanceof Connection) { if ($db instanceof Connection) {
$this->_db = $db; $this->_db = $db;
} else { } else {
throw new Exception("DbCache.connectionID must refer to the ID of a DB connection application component."); throw new InvalidConfigException("DbCache::connectionID must refer to the ID of a DB application component.");
} }
} }
return $this->_db; return $this->_db;
...@@ -163,13 +163,13 @@ class DbCache extends Cache ...@@ -163,13 +163,13 @@ class DbCache extends Cache
*/ */
protected function setValue($key, $value, $expire) protected function setValue($key, $value, $expire)
{ {
$query = new Query; $command = $this->getDb()->createCommand();
$command = $query->update($this->cacheTableName, array( $command->update($this->cacheTableName, array(
'expire' => $expire > 0 ? $expire + time() : 0, 'expire' => $expire > 0 ? $expire + time() : 0,
'data' => array($value, \PDO::PARAM_LOB), 'data' => array($value, \PDO::PARAM_LOB),
), array( ), array(
'id' => $key, 'id' => $key,
))->createCommand($this->getDb()); ));;
if ($command->execute()) { if ($command->execute()) {
$this->gc(); $this->gc();
...@@ -198,16 +198,16 @@ class DbCache extends Cache ...@@ -198,16 +198,16 @@ class DbCache extends Cache
$expire = 0; $expire = 0;
} }
$query = new Query; $command = $this->getDb()->createCommand();
$command = $query->insert($this->cacheTableName, array( $command->insert($this->cacheTableName, array(
'id' => $key, 'id' => $key,
'expire' => $expire, 'expire' => $expire,
'data' => array($value, \PDO::PARAM_LOB), 'data' => array($value, \PDO::PARAM_LOB),
))->createCommand($this->getDb()); ));
try { try {
$command->execute(); $command->execute();
return true; return true;
} catch (Exception $e) { } catch (\Exception $e) {
return false; return false;
} }
} }
...@@ -220,10 +220,8 @@ class DbCache extends Cache ...@@ -220,10 +220,8 @@ class DbCache extends Cache
*/ */
protected function deleteValue($key) protected function deleteValue($key)
{ {
$query = new Query; $command = $this->getDb()->createCommand();
$query->delete($this->cacheTableName, array('id' => $key)) $command->delete($this->cacheTableName, array('id' => $key))->execute();
->createCommand($this->getDb())
->execute();
return true; return true;
} }
...@@ -235,10 +233,8 @@ class DbCache extends Cache ...@@ -235,10 +233,8 @@ class DbCache extends Cache
public function gc($force = false) public function gc($force = false)
{ {
if ($force || mt_rand(0, 1000000) < $this->gcProbability) { if ($force || mt_rand(0, 1000000) < $this->gcProbability) {
$query = new Query; $command = $this->getDb()->createCommand();
$query->delete($this->cacheTableName, 'expire > 0 AND expire < ' . time()) $command->delete($this->cacheTableName, 'expire > 0 AND expire < ' . time())->execute();
->createCommand($this->getDb())
->execute();
} }
} }
...@@ -249,10 +245,8 @@ class DbCache extends Cache ...@@ -249,10 +245,8 @@ class DbCache extends Cache
*/ */
protected function flushValues() protected function flushValues()
{ {
$query = new Query; $command = $this->getDb()->createCommand();
$query->delete($this->cacheTableName) $command->delete($this->cacheTableName)->execute();
->createCommand($this->getDb())
->execute();
return true; return true;
} }
} }
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
namespace yii\caching; namespace yii\caching;
use yii\base\Exception; use yii\base\InvalidConfigException;
use yii\db\Connection; use yii\db\Connection;
use yii\db\Query; use yii\db\Query;
...@@ -68,6 +68,9 @@ class DbDependency extends Dependency ...@@ -68,6 +68,9 @@ class DbDependency extends Dependency
protected function generateDependencyData() protected function generateDependencyData()
{ {
$db = $this->getDb(); $db = $this->getDb();
/**
* @var \yii\db\Command $command
*/
$command = $this->query->createCommand($db); $command = $this->query->createCommand($db);
if ($db->enableQueryCache) { if ($db->enableQueryCache) {
// temporarily disable and re-enable query caching // temporarily disable and re-enable query caching
...@@ -83,7 +86,7 @@ class DbDependency extends Dependency ...@@ -83,7 +86,7 @@ class DbDependency extends Dependency
/** /**
* Returns the DB connection instance used for caching purpose. * Returns the DB connection instance used for caching purpose.
* @return Connection the DB connection instance * @return Connection the DB connection instance
* @throws Exception if [[connectionID]] does not point to a valid application component. * @throws InvalidConfigException if [[connectionID]] does not point to a valid application component.
*/ */
public function getDb() public function getDb()
{ {
...@@ -92,7 +95,7 @@ class DbDependency extends Dependency ...@@ -92,7 +95,7 @@ class DbDependency extends Dependency
if ($db instanceof Connection) { if ($db instanceof Connection) {
$this->_db = $db; $this->_db = $db;
} else { } else {
throw new Exception("DbDependency.connectionID must refer to the ID of a DB connection application component."); throw new InvalidConfigException("DbCache::connectionID must refer to the ID of a DB application component.");
} }
} }
return $this->_db; return $this->_db;
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
namespace yii\caching; namespace yii\caching;
use yii\base\Exception; use yii\base\InvalidConfigException;
/** /**
* FileCache implements a cache component using files. * FileCache implements a cache component using files.
...@@ -42,10 +42,10 @@ class FileCache extends Cache ...@@ -42,10 +42,10 @@ class FileCache extends Cache
public $directoryLevel = 1; public $directoryLevel = 1;
/** /**
* @var integer the probability (parts per million) that garbage collection (GC) should be performed * @var integer the probability (parts per million) that garbage collection (GC) should be performed
* when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance. * when storing a piece of data in the cache. Defaults to 10, meaning 0.001% chance.
* This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all. * This number should be between 0 and 1000000. A value 0 means no GC will be performed at all.
**/ **/
public $gcProbability = 100; public $gcProbability = 10;
/** /**
* Initializes this component by ensuring the existence of the cache path. * Initializes this component by ensuring the existence of the cache path.
...@@ -55,7 +55,7 @@ class FileCache extends Cache ...@@ -55,7 +55,7 @@ class FileCache extends Cache
parent::init(); parent::init();
$this->cachePath = \Yii::getAlias($this->cachePath); $this->cachePath = \Yii::getAlias($this->cachePath);
if ($this->cachePath === false) { if ($this->cachePath === false) {
throw new Exception('FileCache.cachePath must be a valid path alias.'); throw new InvalidConfigException('FileCache.cachePath must be a valid path alias.');
} }
if (!is_dir($this->cachePath)) { if (!is_dir($this->cachePath)) {
mkdir($this->cachePath, 0777, true); mkdir($this->cachePath, 0777, true);
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
namespace yii\caching; namespace yii\caching;
use yii\base\Exception; use yii\base\Exception;
use yii\base\InvalidConfigException;
/** /**
* MemCache implements a cache application component based on [memcache](http://pecl.php.net/package/memcache) * MemCache implements a cache application component based on [memcache](http://pecl.php.net/package/memcache)
...@@ -109,12 +110,12 @@ class MemCache extends Cache ...@@ -109,12 +110,12 @@ class MemCache extends Cache
* @return \Memcache|\Memcached the memcache (or memcached) object used by this cache component. * @return \Memcache|\Memcached the memcache (or memcached) object used by this cache component.
* @throws Exception if memcache or memcached extension is not loaded * @throws Exception if memcache or memcached extension is not loaded
*/ */
public function getMemCache() public function getMemcache()
{ {
if ($this->_cache === null) { if ($this->_cache === null) {
$extension = $this->useMemcached ? 'memcached' : 'memcache'; $extension = $this->useMemcached ? 'memcached' : 'memcache';
if (!extension_loaded($extension)) { if (!extension_loaded($extension)) {
throw new Exception("MemCache requires PHP $extension extension to be loaded."); throw new InvalidConfigException("MemCache requires PHP $extension extension to be loaded.");
} }
$this->_cache = $this->useMemcached ? new \Memcached : new \Memcache; $this->_cache = $this->useMemcached ? new \Memcached : new \Memcache;
} }
......
...@@ -31,7 +31,7 @@ class ZendDataCache extends Cache ...@@ -31,7 +31,7 @@ class ZendDataCache extends Cache
protected function getValue($key) protected function getValue($key)
{ {
$result = zend_shm_cache_fetch($key); $result = zend_shm_cache_fetch($key);
return $result !== NULL ? $result : false; return $result === null ? false : $result;
} }
/** /**
......
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