MDL-44367 filter_data: Moving cache at course level

This commit is contained in:
David Monllao 2015-01-19 14:08:19 +08:00
parent beaceef9ee
commit 9a4ad19f55

View 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