]> git.mjollnir.org Git - moodle.git/commitdiff
Cache context lookup per page for performance
authormoodler <moodler>
Sun, 13 Aug 2006 13:28:01 +0000 (13:28 +0000)
committermoodler <moodler>
Sun, 13 Aug 2006 13:28:01 +0000 (13:28 +0000)
lib/accesslib.php

index 4723cf046c4ad426767ff684323545b18606aad6..368c257e254578423f6f9db2ff8c297a9e17049e 100755 (executable)
@@ -805,14 +805,24 @@ function create_context($level, $instanceid) {
  * @param $instance
  */
 function get_context_instance($level, $instance=SITEID) {
-    // echo "getting level $level instance $instance";
 
-    // XXX TODO  Add caching here
-      if (!$context = get_record('context', 'level', $level, 'instanceid', $instance)) {
-          //echo "creating ...";
+    static $contexts;   // Cache context lookups per page for performance
+
+    if (!isset($contexts)) {
+        $contexts = array();
+    }
+
+    if (isset($contexts[$level][$instance])) {  // Already cached
+        return $contexts[$level][$instance];
+    }
+
+    if (!$context = get_record('context', 'level', $level, 'instanceid', $instance)) {
         create_context($level, $instance);
         $context = get_record('context', 'level', $level, 'instanceid', $instance);
-      }
+    }
+
+    $contexts[$level][$instance] = $context;    // Cache it for later
+
     return $context;
 }