From: stronk7 Date: Fri, 27 Oct 2006 16:56:34 +0000 (+0000) Subject: Minor improvements to the get_record() cache. Part of MDL-7196 X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=9d98635b5ef74220b5a9f01035d4892adf60651a;p=moodle.git Minor improvements to the get_record() cache. Part of MDL-7196 1) Check the cache issset() before unset() it. 2) In the set_field() function, if fieldX = 'id', just delete such element from the cache, else the whole table 3) Add some more unset() operations against the cache in the delete_xxxx() dmllib functions. Merged from MOODLE_17_STABLE --- diff --git a/lib/dmllib.php b/lib/dmllib.php index a4491aa035..97439cbbe1 100644 --- a/lib/dmllib.php +++ b/lib/dmllib.php @@ -1003,7 +1003,7 @@ function get_fieldset_sql($sql) { */ function set_field($table, $newfield, $newvalue, $field1, $value1, $field2='', $value2='', $field3='', $value3='') { - global $db, $CFG; + global $db, $CFG, $record_cache; if (defined('MDL_PERFDB')) { global $PERF ; $PERF->dbqueries++; }; @@ -1041,9 +1041,24 @@ function set_field($table, $newfield, $newvalue, $field1, $value1, $field2='', $ } } - // Clear entire record cache for table (could be improved to check for ID limitation) - global $record_cache; - unset($record_cache[$table]); + // Clear record_cache based on the parameters passed (individual record or whole table) + if ($field1 == 'id') { + if (isset($record_cache[$table][$value1])) { + unset($record_cache[$table][$value1]); + } + } else if ($field2 == 'id') { + if (isset($record_cache[$table][$value2])) { + unset($record_cache[$table][$value2]); + } + } else if ($field3 == 'id') { + if (isset($record_cache[$table][$value3])) { + unset($record_cache[$table][$value3]); + } + } else { + if (isset($record_cache[$table])) { + unset($record_cache[$table]); + } + } /// Arriving here, standard update return $db->Execute('UPDATE '. $CFG->prefix . $table .' SET '. $newfield .' = \''. $newvalue .'\' '. $select); @@ -1067,6 +1082,25 @@ function delete_records($table, $field1='', $value1='', $field2='', $value2='', global $db, $CFG; + // Clear record_cache based on the parameters passed (individual record or whole table) + if ($field1 == 'id') { + if (isset($record_cache[$table][$value1])) { + unset($record_cache[$table][$value1]); + } + } else if ($field2 == 'id') { + if (isset($record_cache[$table][$value2])) { + unset($record_cache[$table][$value2]); + } + } else if ($field3 == 'id') { + if (isset($record_cache[$table][$value3])) { + unset($record_cache[$table][$value3]); + } + } else { + if (isset($record_cache[$table])) { + unset($record_cache[$table]); + } + } + if (defined('MDL_PERFDB')) { global $PERF ; $PERF->dbqueries++; }; $select = where_clause($field1, $value1, $field2, $value2, $field3, $value3); @@ -1094,6 +1128,11 @@ function delete_records_select($table, $select='') { $select = 'WHERE '.$select; } + // Clear record_cache (whole table) + if (isset($record_cache[$table])) { + unset($record_cache[$table]); + } + return $db->Execute('DELETE FROM '. $CFG->prefix . $table .' '. $select); } @@ -1293,14 +1332,13 @@ function insert_record($table, $dataobject, $returnid=true, $primarykey='id') { */ function update_record($table, $dataobject) { - global $db, $CFG; + global $db, $CFG, $record_cache; if (! isset($dataobject->id) ) { return false; } // Remove this record from record cache since it will change - global $record_cache; if (isset($record_cache[$table][$dataobject->id])) { unset($record_cache[$table][$dataobject->id]); }