From 1336c96edb50f48d5d31627cddb1733667414fea Mon Sep 17 00:00:00 2001 From: skodak Date: Sun, 6 Jul 2008 18:58:46 +0000 Subject: [PATCH] MDL-15553 events_trigger() perf improvement; merged from MOODLE_19_STABLE --- lib/eventslib.php | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/eventslib.php b/lib/eventslib.php index f445ad2aed0..e295d0f40f8 100755 --- a/lib/eventslib.php +++ b/lib/eventslib.php @@ -325,7 +325,27 @@ function events_dequeue($qhandler) { } } +/** + * Returns hanflers for given event. Uses caching for better perf. + * @param string $eventanme name of even or 'reset' + * @return mixed array of handlers or false otherwise + * + * INTERNAL - to be used from eventslib only + */ +function events_get_handlers($eventname) { + static $handlers = array(); + if ($eventname == 'reset') { + $handlers = array(); + return false; + } + + if (!array_key_exists($eventname, $handlers)) { + $handlers[$eventname] = $DB->get_records('events_handlers', array('eventname'=>$eventname)); + } + + return $handlers[$eventname]; +} /****** Public events API starts here, do not use functions above in 3rd party code ******/ @@ -390,7 +410,7 @@ function events_trigger($eventname, $eventdata) { $event = false; // pull out all registered event handlers - if ($handlers = $DB->get_records('events_handlers', array('eventname'=>$eventname))) { + if ($handlers = events_get_handlers($eventname)) { foreach ($handlers as $handler) { $errormessage = '';