MDL-25290 conversion: Added db column cache to pgsql + mysql

This commit is contained in:
Sam Hemelryk 2012-08-14 11:38:13 +12:00
parent 7e8ae12a7a
commit 9381983e5b
3 changed files with 48 additions and 11 deletions

View File

@ -118,6 +118,9 @@ abstract class moodle_database {
/** @var bool Flag used to force rollback of all current transactions. */
private $force_rollback = false;
/** @var string MD5 of settings used for connection. Used by MUC as an identifier. */
private $settingshash;
/**
* @var int internal temporary variable used to fix params. Its used by {@link _fix_sql_params_dollar_callback()}.
*/
@ -288,6 +291,20 @@ abstract class moodle_database {
$this->dboptions = (array)$dboptions;
}
/**
* Returns a hash for the settings used during connection.
*
* If not already requested it is generated and stored in a private property.
*
* @return string
*/
protected function get_settings_hash() {
if (empty($this->settingshash)) {
$this->settingshash = md5($this->dbhost . $this->dbuser . $this->prefix);
}
return $this->settingshash;
}
/**
* Attempt to create the database
* @param string $dbhost The database host.
@ -909,6 +926,9 @@ abstract class moodle_database {
public function reset_caches() {
$this->columns = array();
$this->tables = null;
// Purge MUC as well
$identifiers = array('dbfamily' => $this->get_dbfamily(), 'settings' => $this->get_settings_hash());
cache_helper::purge_by_definition('core', 'databasemeta', $identifiers);
}
/**

View File

@ -515,11 +515,16 @@ class mysqli_native_moodle_database extends moodle_database {
* @return array array of database_column_info objects indexed with column names
*/
public function get_columns($table, $usecache=true) {
if ($usecache and isset($this->columns[$table])) {
return $this->columns[$table];
if ($usecache) {
$properties = array('dbfamily' => $this->get_dbfamily(), 'settings' => $this->get_settings_hash());
$cache = cache::make('core', 'databasemeta', $properties);
if ($data = $cache->get($table)) {
return $data;
}
}
$this->columns[$table] = array();
$structure = array();
$sql = "SELECT column_name, data_type, character_maximum_length, numeric_precision,
numeric_scale, is_nullable, column_type, column_default, column_key, extra
@ -539,7 +544,7 @@ class mysqli_native_moodle_database extends moodle_database {
// standard table exists
while ($rawcolumn = $result->fetch_assoc()) {
$info = (object)$this->get_column_info((object)$rawcolumn);
$this->columns[$table][$info->name] = new database_column_info($info);
$structure[$info->name] = new database_column_info($info);
}
$result->close();
@ -614,12 +619,16 @@ class mysqli_native_moodle_database extends moodle_database {
}
$info = $this->get_column_info($rawcolumn);
$this->columns[$table][$info->name] = new database_column_info($info);
$structure[$info->name] = new database_column_info($info);
}
$result->close();
}
return $this->columns[$table];
if ($usecache) {
$result = $cache->set($table, $structure);
}
return $structure;
}
/**

View File

@ -395,11 +395,15 @@ class pgsql_native_moodle_database extends moodle_database {
* @return array array of database_column_info objects indexed with column names
*/
public function get_columns($table, $usecache=true) {
if ($usecache and isset($this->columns[$table])) {
return $this->columns[$table];
if ($usecache) {
$properties = array('dbfamily' => $this->get_dbfamily(), 'settings' => $this->get_settings_hash());
$cache = cache::make('core', 'databasemeta', $properties);
if ($data = $cache->get($table)) {
return $data;
}
}
$this->columns[$table] = array();
$structure = array();
$tablename = $this->prefix.$table;
@ -569,12 +573,16 @@ class pgsql_native_moodle_database extends moodle_database {
}
$this->columns[$table][$info->name] = new database_column_info($info);
$structure[$info->name] = new database_column_info($info);
}
pg_free_result($result);
return $this->columns[$table];
if ($usecache) {
$result = $cache->set($table, $structure);
}
return $structure;
}
/**