From e5605780cfd0992bdd47e2ab28f8c983eb7dabf5 Mon Sep 17 00:00:00 2001 From: moodler Date: Sun, 13 Aug 2006 13:28:01 +0000 Subject: [PATCH] Cache context lookup per page for performance --- lib/accesslib.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/accesslib.php b/lib/accesslib.php index 4723cf046c..368c257e25 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -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; } -- 2.39.5