diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index ef53f0830a7..b261bb1f74a 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -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); } /** diff --git a/lib/dml/mysqli_native_moodle_database.php b/lib/dml/mysqli_native_moodle_database.php index c7f83fcf21b..44f3a92562e 100644 --- a/lib/dml/mysqli_native_moodle_database.php +++ b/lib/dml/mysqli_native_moodle_database.php @@ -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; } /** diff --git a/lib/dml/pgsql_native_moodle_database.php b/lib/dml/pgsql_native_moodle_database.php index 62d754895c8..ab3650fcaf3 100644 --- a/lib/dml/pgsql_native_moodle_database.php +++ b/lib/dml/pgsql_native_moodle_database.php @@ -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; } /**