Permalink
Browse files

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

  • Loading branch information...
1 parent 7e8ae12 commit 9381983e5b260a7050d0ec322e10f974f1070872 Sam Hemelryk committed Aug 13, 2012
Showing with 48 additions and 11 deletions.
  1. +20 −0 lib/dml/moodle_database.php
  2. +15 −6 lib/dml/mysqli_native_moodle_database.php
  3. +13 −5 lib/dml/pgsql_native_moodle_database.php
@@ -118,6 +118,9 @@
/** @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()}.
*/
@@ -289,6 +292,20 @@ protected function store_settings($dbhost, $dbuser, $dbpass, $dbname, $prefix, a
}
/**
+ * 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.
* @param string $dbuser The database user to connect as.
@@ -909,6 +926,9 @@ public function fix_sql_params($sql, array $params=null) {
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);
}
/**
@@ -515,11 +515,16 @@ public function get_indexes($table) {
* @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 @@ public function get_columns($table, $usecache=true) {
// 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 @@ public function get_columns($table, $usecache=true) {
}
$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;
}
/**
@@ -395,11 +395,15 @@ public function get_indexes($table) {
* @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 @@ public function get_columns($table, $usecache=true) {
}
- $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;
}
/**

0 comments on commit 9381983

Please sign in to comment.