Commit 11d374a8 by Qiang Xue

finished cleanup of caching components.

parent ce168fb3
......@@ -81,9 +81,9 @@ class ChainedDependency extends Dependency
*/
public function getHasChanged()
{
foreach ($this->dependencies as $dependency) {
foreach ($this->dependencies as $i => $dependency) {
if (!$dependency instanceof Dependency) {
$dependency = \Yii::createObject($dependency);
$this->dependencies[$i] = $dependency = \Yii::createObject($dependency);
}
if ($this->dependOnAll && $dependency->getHasChanged()) {
return true;
......
......@@ -9,7 +9,7 @@
namespace yii\caching;
use yii\base\Exception;
use yii\base\InvalidConfigException;
use yii\db\Connection;
use yii\db\Query;
......@@ -21,11 +21,11 @@ use yii\db\Query;
*
* ~~~
* CREATE TABLE tbl_cache (
* id char(128) NOT NULL,
* expire int(11) DEFAULT NULL,
* data LONGBLOB,
* PRIMARY KEY (id),
* KEY expire (expire)
* id char(128) NOT NULL,
* expire int(11) DEFAULT NULL,
* data LONGBLOB,
* PRIMARY KEY (id),
* KEY expire (expire)
* );
* ~~~
*
......@@ -57,7 +57,7 @@ class DbCache extends Cache
public $cacheTableName = 'tbl_cache';
/**
* @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.
**/
public $gcProbability = 100;
......@@ -69,7 +69,7 @@ class DbCache extends Cache
/**
* Returns the DB connection instance used for caching purpose.
* @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()
{
......@@ -78,7 +78,7 @@ class DbCache extends Cache
if ($db instanceof Connection) {
$this->_db = $db;
} 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;
......@@ -163,13 +163,13 @@ class DbCache extends Cache
*/
protected function setValue($key, $value, $expire)
{
$query = new Query;
$command = $query->update($this->cacheTableName, array(
$command = $this->getDb()->createCommand();
$command->update($this->cacheTableName, array(
'expire' => $expire > 0 ? $expire + time() : 0,
'data' => array($value, \PDO::PARAM_LOB),
), array(
'id' => $key,
))->createCommand($this->getDb());
));;
if ($command->execute()) {
$this->gc();
......@@ -198,16 +198,16 @@ class DbCache extends Cache
$expire = 0;
}
$query = new Query;
$command = $query->insert($this->cacheTableName, array(
$command = $this->getDb()->createCommand();
$command->insert($this->cacheTableName, array(
'id' => $key,
'expire' => $expire,
'data' => array($value, \PDO::PARAM_LOB),
))->createCommand($this->getDb());
));
try {
$command->execute();
return true;
} catch (Exception $e) {
} catch (\Exception $e) {
return false;
}
}
......@@ -220,10 +220,8 @@ class DbCache extends Cache
*/
protected function deleteValue($key)
{
$query = new Query;
$query->delete($this->cacheTableName, array('id' => $key))
->createCommand($this->getDb())
->execute();
$command = $this->getDb()->createCommand();
$command->delete($this->cacheTableName, array('id' => $key))->execute();
return true;
}
......@@ -235,10 +233,8 @@ class DbCache extends Cache
public function gc($force = false)
{
if ($force || mt_rand(0, 1000000) < $this->gcProbability) {
$query = new Query;
$query->delete($this->cacheTableName, 'expire > 0 AND expire < ' . time())
->createCommand($this->getDb())
->execute();
$command = $this->getDb()->createCommand();
$command->delete($this->cacheTableName, 'expire > 0 AND expire < ' . time())->execute();
}
}
......@@ -249,10 +245,8 @@ class DbCache extends Cache
*/
protected function flushValues()
{
$query = new Query;
$query->delete($this->cacheTableName)
->createCommand($this->getDb())
->execute();
$command = $this->getDb()->createCommand();
$command->delete($this->cacheTableName)->execute();
return true;
}
}
......@@ -9,7 +9,7 @@
namespace yii\caching;
use yii\base\Exception;
use yii\base\InvalidConfigException;
use yii\db\Connection;
use yii\db\Query;
......@@ -68,6 +68,9 @@ class DbDependency extends Dependency
protected function generateDependencyData()
{
$db = $this->getDb();
/**
* @var \yii\db\Command $command
*/
$command = $this->query->createCommand($db);
if ($db->enableQueryCache) {
// temporarily disable and re-enable query caching
......@@ -83,7 +86,7 @@ class DbDependency extends Dependency
/**
* Returns the DB connection instance used for caching purpose.
* @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()
{
......@@ -92,7 +95,7 @@ class DbDependency extends Dependency
if ($db instanceof Connection) {
$this->_db = $db;
} 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;
......
......@@ -9,7 +9,7 @@
namespace yii\caching;
use yii\base\Exception;
use yii\base\InvalidConfigException;
/**
* FileCache implements a cache component using files.
......@@ -42,10 +42,10 @@ class FileCache extends Cache
public $directoryLevel = 1;
/**
* @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.
* This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all.
* 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 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.
......@@ -55,7 +55,7 @@ class FileCache extends Cache
parent::init();
$this->cachePath = \Yii::getAlias($this->cachePath);
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)) {
mkdir($this->cachePath, 0777, true);
......
......@@ -10,6 +10,7 @@
namespace yii\caching;
use yii\base\Exception;
use yii\base\InvalidConfigException;
/**
* MemCache implements a cache application component based on [memcache](http://pecl.php.net/package/memcache)
......@@ -109,12 +110,12 @@ class MemCache extends Cache
* @return \Memcache|\Memcached the memcache (or memcached) object used by this cache component.
* @throws Exception if memcache or memcached extension is not loaded
*/
public function getMemCache()
public function getMemcache()
{
if ($this->_cache === null) {
$extension = $this->useMemcached ? 'memcached' : 'memcache';
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;
}
......
......@@ -31,7 +31,7 @@ class ZendDataCache extends Cache
protected function getValue($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