MDL-44367 filter_data: Moving cache at course level
authorDavid Monllao <davidm@moodle.com>
Mon, 19 Jan 2015 06:08:19 +0000 (14:08 +0800)
committerDavid Monllao <davidm@moodle.com>
Wed, 11 Mar 2015 01:54:25 +0000 (09:54 +0800)
filter/data/filter.php

index e703d92..4eabac0 100644 (file)
@@ -32,26 +32,36 @@ defined('MOODLE_INTERNAL') || die();
 class filter_data extends moodle_text_filter {
 
     public function filter($text, array $options = array()) {
-        global $CFG, $DB;
+        global $CFG, $DB, $USER;
 
-        // Trivial-cache - keyed on $cachedcontextid
-        static $cachedcontextid;
-        static $contentlist;
+        // Trivial-cache - keyed on $cachedcourseid + $cacheduserid.
+        static $cachedcourseid = null;
+        static $cacheduserid = null;
+        static $coursecontentlist = array();
+        static $sitecontentlist = array();
 
         static $nothingtodo;
 
         // Try to get current course.
         $coursectx = $this->context->get_course_context(false);
         if (!$coursectx) {
+            // We could be in a course category so no entries for courseid == 0 will be found.
             $courseid = 0;
         } else {
             $courseid = $coursectx->instanceid;
         }
 
-        // Initialise/invalidate our trivial cache if dealing with a different context
-        if (!isset($cachedcontextid) || $cachedcontextid !== $this->context->id) {
-            $cachedcontextid = $this->context->id;
-            $contentlist = array();
+        if ($cacheduserid !== $USER->id) {
+            // Invalidate all caches if the user changed.
+            $coursecontentlist = array();
+            $sitecontentlist = array();
+            $cacheduserid = $USER->id;
+            $cachedcourseid = $courseid;
+            $nothingtodo = false;
+        } else if ($courseid != get_site()->id && $courseid != 0 && $cachedcourseid != $courseid) {
+            // Invalidate course-level caches if the course id changed.
+            $coursecontentlist = array();
+            $cachedcourseid = $courseid;
             $nothingtodo = false;
         }
 
@@ -59,6 +69,13 @@ class filter_data extends moodle_text_filter {
             return $text;
         }
 
+        // If courseid == 0 only site entries will be returned.
+        if ($courseid == get_site()->id || $courseid == 0) {
+            $contentlist = & $sitecontentlist;
+        } else {
+            $contentlist = & $coursecontentlist;
+        }
+
         // Create a list of all the resources to search for. It may be cached already.
         if (empty($contentlist)) {
             $coursestosearch = $courseid ? array($courseid) : array(); // Add courseid if found