Commit 9d75714b by Qiang Xue

refactored query caching and schema caching.

parent 8517ee9e
...@@ -365,13 +365,12 @@ class Command extends \yii\base\Component ...@@ -365,13 +365,12 @@ class Command extends \yii\base\Component
} }
\Yii::trace("Querying SQL: {$sql}{$paramLog}", __CLASS__); \Yii::trace("Querying SQL: {$sql}{$paramLog}", __CLASS__);
echo $sql."\n\n";
if ($db->queryCachingCount > 0 && $db->queryCachingDuration >= 0 && $method !== '') { if ($db->enableQueryCache && $method !== '') {
$cache = \Yii::$application->getComponent($db->queryCacheID); $cache = \Yii::$application->getComponent($db->queryCacheID);
} }
if (isset($cache)) { if (isset($cache)) {
$db->queryCachingCount--;
$cacheKey = __CLASS__ . "/{$db->dsn}/{$db->username}/$sql/$paramLog"; $cacheKey = __CLASS__ . "/{$db->dsn}/{$db->username}/$sql/$paramLog";
if (($result = $cache->get($cacheKey)) !== false) { if (($result = $cache->get($cacheKey)) !== false) {
\Yii::trace('Query result found in cache', __CLASS__); \Yii::trace('Query result found in cache', __CLASS__);
......
...@@ -130,62 +130,63 @@ class Connection extends \yii\base\ApplicationComponent ...@@ -130,62 +130,63 @@ class Connection extends \yii\base\ApplicationComponent
*/ */
public $pdo; public $pdo;
/** /**
* @var boolean whether to enable schema caching.
* Note that in order to enable truly schema caching, a valid cache component as specified
* by [[schemaCacheID]] must be enabled and [[schemaCacheEnabled]] must be set true.
* @see schemaCacheDuration
* @see schemaCacheExclude
* @see schemaCacheID
*/
public $enableSchemaCache = false;
/**
* @var integer number of seconds that table metadata can remain valid in cache. * @var integer number of seconds that table metadata can remain valid in cache.
* Defaults to -1, meaning schema caching is disabled.
* Use 0 to indicate that the cached data will never expire. * Use 0 to indicate that the cached data will never expire.
* * @see enableSchemaCache
* Note that in order to enable schema caching, a valid cache component as specified
* by [[schemaCacheID]] must be enabled.
* @see schemaCachingExclude
* @see schemaCacheID
*/ */
public $schemaCachingDuration = -1; public $schemaCacheDuration = 3600;
/** /**
* @var array list of tables whose metadata should NOT be cached. Defaults to empty array. * @var array list of tables whose metadata should NOT be cached. Defaults to empty array.
* The table names may contain schema prefix, if any. Do not quote the table names. * The table names may contain schema prefix, if any. Do not quote the table names.
* @see schemaCachingDuration * @see enableSchemaCache
*/ */
public $schemaCachingExclude = array(); public $schemaCacheExclude = array();
/** /**
* @var string the ID of the cache application component that is used to cache the table metadata. * @var string the ID of the cache application component that is used to cache the table metadata.
* Defaults to 'cache'. * Defaults to 'cache'.
* @see schemaCachingDuration * @see enableSchemaCache
*/ */
public $schemaCacheID = 'cache'; public $schemaCacheID = 'cache';
/** /**
* @var integer number of seconds that query results can remain valid in cache. * @var boolean whether to enable query caching.
* Defaults to -1, meaning query caching is disabled.
* Use 0 to indicate that the cached data will never expire.
*
* Note that in order to enable query caching, a valid cache component as specified * Note that in order to enable query caching, a valid cache component as specified
* by [[queryCacheID]] must be enabled. * by [[queryCacheID]] must be enabled and [[queryCacheEnabled]] must be set true.
*
* The method [[cache()]] is provided as a convenient way of setting this property
* and [[queryCachingDependency]] on the fly.
* *
* @see cache * Methods [[beginCache()]] and [[endCache()]] can be used as shortcuts to turn on
* @see queryCachingDependency * and off query caching on the fly.
* @see queryCacheDuration
* @see queryCacheID * @see queryCacheID
* @see queryCacheDependency
* @see beginCache()
* @see endCache()
*/ */
public $queryCachingDuration = -1; public $enableQueryCache = false;
/** /**
* @var \yii\caching\Dependency the dependency that will be used when saving query results into cache. * @var integer number of seconds that query results can remain valid in cache.
* Defaults to null, meaning no dependency. * Defaults to 3600, meaning one hour.
* @see queryCachingDuration * Use 0 to indicate that the cached data will never expire.
* @see enableQueryCache
*/ */
public $queryCachingDependency; public $queryCacheDuration = 3600;
/** /**
* @var integer the number of SQL statements that need to be cached when they are executed next. * @var \yii\caching\Dependency the dependency that will be used when saving query results into cache.
* Defaults to 0, meaning the query result of the next SQL statement will NOT be cached. * Defaults to null, meaning no dependency.
* Note that each time after executing a SQL statement (whether executed on DB server or fetched from * @see enableQueryCache
* query cache), this property will be reduced by 1 until 0.
* @see queryCachingDuration
*/ */
public $queryCachingCount = 0; public $queryCacheDependency;
/** /**
* @var string the ID of the cache application component that is used for query caching. * @var string the ID of the cache application component that is used for query caching.
* Defaults to 'cache'. * Defaults to 'cache'.
* @see queryCachingDuration * @see enableQueryCache
*/ */
public $queryCacheID = 'cache'; public $queryCacheID = 'cache';
/** /**
...@@ -297,24 +298,29 @@ class Connection extends \yii\base\ApplicationComponent ...@@ -297,24 +298,29 @@ class Connection extends \yii\base\ApplicationComponent
} }
/** /**
* Sets the parameters about query caching. * Turns on query caching.
* This method is provided as a shortcut to setting three properties that are related * This method is provided as a shortcut to setting two properties that are related
* with query caching: [[queryCachingDuration]], [[queryCachingDependency]] and * with query caching: [[queryCacheDuration]] and [[queryCacheDependency]].
* [[queryCachingCount]].
* @param integer $duration the number of seconds that query results may remain valid in cache. * @param integer $duration the number of seconds that query results may remain valid in cache.
* See [[queryCachingDuration]] for more details. * See [[queryCacheDuration]] for more details.
* @param \yii\caching\Dependency $dependency the dependency for the cached query result. * @param \yii\caching\Dependency $dependency the dependency for the cached query result.
* See [[queryCachingDependency]] for more details. * See [[queryCacheDependency]] for more details.
* @param integer $queryCount the number of SQL queries that need to be cached after calling this method. */
* See [[queryCachingCount]] for more details. public function beginCache($duration = null, $dependency = null)
* @return Connection the connection instance itself. {
$this->enableQueryCache = true;
if ($duration !== null) {
$this->queryCacheDuration = $duration;
}
$this->queryCacheDependency = $dependency;
}
/**
* Turns off query caching.
*/ */
public function cache($duration = 300, $dependency = null, $queryCount = 1) public function endCache()
{ {
$this->queryCachingDuration = $duration; $this->enableQueryCache = false;
$this->queryCachingDependency = $dependency;
$this->queryCachingCount = $queryCount;
return $this;
} }
/** /**
......
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