Index: lib/dmllib.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/dmllib.php,v retrieving revision 1.53 diff -u -r1.53 dmllib.php --- lib/dmllib.php 23 Oct 2006 17:46:08 -0000 1.53 +++ lib/dmllib.php 24 Oct 2006 10:44:08 -0000 @@ -354,6 +354,11 @@ /// GENERIC FUNCTIONS TO GET, INSERT, OR UPDATE DATA /////////////////////////////////// +// This cache (enabled by setting $CFG['cachegetrecord']) stores the results of +// get_record calls within a request, where all fields of a record were requested and +// the only condition was id=something. +$recordcache=array(); + /** * Get a single record as an object * @@ -368,12 +373,36 @@ * @return mixed a fieldset object containing the first mathcing record, or false if none found. */ function get_record($table, $field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields='*') { - - global $CFG; - + + global $CFG,$recordcache; + + // Check to see whether this record is eligible for caching (fields=*, only condition is id) + // and cache is turned on + $docache=false; + if(!empty($CFG->cachegetrecord) && $field1=='id' && !$field2 && !$field3 && $fields=='*') { + $docache=true; + // If it's in the cache, return it + if(array_key_exists($table,$recordcache)) { + if(array_key_exists($value1,$recordcache[$table])) { + return $recordcache[$table][$value1]; + } + } + } + $select = where_clause($field1, $value1, $field2, $value2, $field3, $value3); - return get_record_sql('SELECT '.$fields.' FROM '. $CFG->prefix . $table .' '. $select); + $record=get_record_sql('SELECT '.$fields.' FROM '. $CFG->prefix . $table .' '. $select); + + // If we're caching records, store this one (supposing we got something - we don't cache + // failures) + if($record && $docache) { + if(!array_key_exists($table,$recordcache)) { + $recordcache[$table]=array(); + } + $recordcache[$table][$value1]=$record; + } + + return $record; } /** @@ -1022,6 +1051,10 @@ } } + // Clear entire record cache for table (could be improved to check for ID limitation) + global $recordcache; + unset($recordcache[$table]); + /// Arriving here, standard update return $db->Execute('UPDATE '. $CFG->prefix . $table .' SET '. $newfield .' = \''. $newvalue .'\' '. $select); } @@ -1276,6 +1309,10 @@ return false; } + // Remove this record from record cache since it will change + global $recordcache; + unset($recordcache[$table][$dataobject->id]); + /// Temporary hack as part of phasing out all access to obsolete user tables XXX if (!empty($CFG->rolesactive)) { if (in_array($table, array('user_students', 'user_teachers', 'user_coursecreators', 'user_admins'))) {