Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch '2.0-dbo-performance' into 2.0

  • Loading branch information...
commit 420a2f56df47f474d5d1d4acd24b9e6d96028ccf 2 parents 4ae9058 + 50d4330
@lorenzo lorenzo authored
View
2  lib/Cake/Model/Datasource/Database/Mysql.php
@@ -238,7 +238,7 @@ public function fetchResult() {
foreach ($this->map as $col => $meta) {
list($table, $column, $type) = $meta;
$resultRow[$table][$column] = $row[$col];
- if ($type === 'boolean' && !is_null($row[$col])) {
+ if ($type === 'boolean' && $row[$col] !== null) {
$resultRow[$table][$column] = $this->boolean($resultRow[$table][$column]);
}
}
View
75 lib/Cake/Model/Datasource/DboSource.php
@@ -62,7 +62,7 @@ class DboSource extends DataSource {
* @var array
* @access public
*/
- public $methodCache = array();
+ public static $methodCache = array();
/**
* Whether or not to cache the results of DboSource::name() and DboSource::conditions()
@@ -247,6 +247,14 @@ class DboSource extends DataSource {
public $fieldParameters = array();
/**
+ * Indicates whether there was a change on the cached results on the methods of this class
+ * This will be used for storing in a more persistent cache
+ *
+ * @var boolean
+ */
+ protected $_methodCacheChange = false;
+
+/**
* Constructor
*
* @param array $config Array of configuration information for the Datasource.
@@ -745,7 +753,8 @@ public function field($name, $sql) {
* @return void
*/
public function flushMethodCache() {
- $this->methodCache = array();
+ $this->_methodCacheChange = true;
+ self::$methodCache = array();
}
/**
@@ -764,10 +773,14 @@ public function cacheMethod($method, $key, $value = null) {
if ($this->cacheMethods === false) {
return $value;
}
+ if (empty(self::$methodCache)) {
+ self::$methodCache = Cache::read('method_cache', '_cake_core_');
+ }
if ($value === null) {
- return (isset($this->methodCache[$method][$key])) ? $this->methodCache[$method][$key] : null;
+ return (isset(self::$methodCache[$method][$key])) ? self::$methodCache[$method][$key] : null;
}
- return $this->methodCache[$method][$key] = $value;
+ $this->_methodCacheChange = true;
+ return self::$methodCache[$method][$key] = $value;
}
/**
@@ -1460,7 +1473,7 @@ public function generateAssociationQuery($model, $linkModel, $type, $association
$queryData['fields'] = $this->fields($model, $modelAlias);
} elseif (!empty($model->hasMany) && $model->recursive > -1) {
$assocFields = $this->fields($model, $modelAlias, array("{$modelAlias}.{$model->primaryKey}"));
- $passedFields = $this->fields($model, $modelAlias, $queryData['fields']);
+ $passedFields = $queryData['fields'];
if (count($passedFields) === 1) {
if (strpos($passedFields[0], $assocFields[0]) === false && !preg_match('/^[a-z]+\(/i', $passedFields[0])) {
$queryData['fields'] = array_merge($passedFields, $assocFields);
@@ -2167,17 +2180,16 @@ public function fields($model, $alias = null, $fields = array(), $quote = true)
if (empty($alias)) {
$alias = $model->alias;
}
+ $virtualFields = $model->getVirtualField();
$cacheKey = array(
- $model->useDbConfig,
- $model->table,
- array_keys($model->schema()),
- $model->name,
- $model->getVirtualField(),
$alias,
+ get_class($model),
+ $model->alias,
+ $virtualFields,
$fields,
$quote
);
- $cacheKey = crc32(serialize($cacheKey));
+ $cacheKey = md5(serialize($cacheKey));
if ($return = $this->cacheMethod(__FUNCTION__, $cacheKey)) {
return $return;
}
@@ -2191,7 +2203,6 @@ public function fields($model, $alias = null, $fields = array(), $quote = true)
$allFields = $allFields || in_array('*', $fields) || in_array($model->alias . '.*', $fields);
$virtual = array();
- $virtualFields = $model->getVirtualField();
if (!empty($virtualFields)) {
$virtualKeys = array_keys($virtualFields);
foreach ($virtualKeys as $field) {
@@ -2287,25 +2298,6 @@ public function fields($model, $alias = null, $fields = array(), $quote = true)
* @return string SQL fragment
*/
public function conditions($conditions, $quoteValues = true, $where = true, $model = null) {
- if (is_object($model)) {
- $cacheKey = array(
- $model->useDbConfig,
- $model->table,
- $model->schema(),
- $model->name,
- $model->getVirtualField(),
- $conditions,
- $quoteValues,
- $where
- );
- } else {
- $cacheKey = array($conditions, $quoteValues, $where);
- }
- $cacheKey = crc32(serialize($cacheKey));
- if ($return = $this->cacheMethod(__FUNCTION__, $cacheKey)) {
- return $return;
- }
-
$clause = $out = '';
if ($where) {
@@ -2316,16 +2308,16 @@ public function conditions($conditions, $quoteValues = true, $where = true, $mod
$out = $this->conditionKeysToString($conditions, $quoteValues, $model);
if (empty($out)) {
- return $this->cacheMethod(__FUNCTION__, $cacheKey, $clause . ' 1 = 1');
+ return $clause . ' 1 = 1';
}
- return $this->cacheMethod(__FUNCTION__, $cacheKey, $clause . implode(' AND ', $out));
+ return $clause . implode(' AND ', $out);
}
if (is_bool($conditions)) {
- return $this->cacheMethod(__FUNCTION__, $cacheKey, $clause . (int)$conditions . ' = 1');
+ return $clause . (int)$conditions . ' = 1';
}
if (empty($conditions) || trim($conditions) === '') {
- return $this->cacheMethod(__FUNCTION__, $cacheKey, $clause . '1 = 1');
+ return $clause . '1 = 1';
}
$clauses = '/^WHERE\\x20|^GROUP\\x20BY\\x20|^HAVING\\x20|^ORDER\\x20BY\\x20/i';
@@ -2333,7 +2325,7 @@ public function conditions($conditions, $quoteValues = true, $where = true, $mod
$clause = '';
}
$conditions = $this->__quoteFields($conditions);
- return $this->cacheMethod(__FUNCTION__, $cacheKey, $clause . $conditions);
+ return $clause . $conditions;
}
/**
@@ -3142,4 +3134,15 @@ public function getQueryCache($sql, $params = array()) {
return false;
}
+/**
+ * Used for storing in cache the results of the in-memory methodCache
+ *
+ * @return void
+ */
+ public function __destruct() {
+ if ($this->_methodCacheChange) {
+ Cache::write('method_cache', self::$methodCache, '_cake_core_');
+ }
+ }
+
}
View
2  lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php
@@ -1609,7 +1609,7 @@ public function testGenerateAssociationQueryHasManyAndAggregateFunction() {
$this->_buildRelatedModels($this->Model);
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
- $queryData = array('fields' => array('MIN(TestModel5.test_model4_id)'));
+ $queryData = array('fields' => array('MIN(`TestModel5`.`test_model4_id`)'));
$resultSet = null;
$null = null;
View
1  lib/Cake/Test/Case/Model/ModelWriteTest.php
@@ -413,6 +413,7 @@ public function testCounterCacheWithSelfJoin() {
}
$column .= $this->db->buildColumn(array('name' => 'child_count', 'type' => 'integer'));
$this->db->query('ALTER TABLE '. $this->db->fullTableName('category_threads') . ' ADD ' . $column);
+ $this->db->flushMethodCache();
$Category = new CategoryThread();
$result = $Category->updateAll(array('CategoryThread.name' => "'updated'"), array('CategoryThread.parent_id' => 5));
$this->assertFalse(empty($result));
Please sign in to comment.
Something went wrong with that request. Please try again.