]> git.mjollnir.org Git - moodle.git/commitdiff
Minor improvements to the get_record() cache. Part of MDL-7196
authorstronk7 <stronk7>
Fri, 27 Oct 2006 16:56:34 +0000 (16:56 +0000)
committerstronk7 <stronk7>
Fri, 27 Oct 2006 16:56:34 +0000 (16:56 +0000)
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

lib/dmllib.php

index a4491aa035c4d7f64b57b1573277d40c7a8ae015..97439cbbe1d583f9944b2ab182877ae75f4cd60e 100644 (file)
@@ -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]);
     }