MDL-72837 core_cache: Use versioned cache for modinfo
Uses the new versioned cache feature for modinfo, which should make it safe as a localisable cache.
This commit is contained in:
+10
-15
@@ -475,14 +475,14 @@ class course_modinfo {
|
||||
$cachecoursemodinfo = cache::make('core', 'coursemodinfo');
|
||||
|
||||
// Retrieve modinfo from cache. If not present or cacherev mismatches, call rebuild and retrieve again.
|
||||
$coursemodinfo = $cachecoursemodinfo->get($course->id);
|
||||
if ($coursemodinfo === false || ($course->cacherev != $coursemodinfo->cacherev)) {
|
||||
$coursemodinfo = $cachecoursemodinfo->get_versioned($course->id, $course->cacherev);
|
||||
if (!$coursemodinfo) {
|
||||
$lock = self::get_course_cache_lock($course->id);
|
||||
try {
|
||||
// Only actually do the build if it's still needed after getting the lock (not if
|
||||
// somebody else, who might have been holding the lock, built it already).
|
||||
$coursemodinfo = $cachecoursemodinfo->get($course->id);
|
||||
if ($coursemodinfo === false || ($course->cacherev != $coursemodinfo->cacherev)) {
|
||||
$coursemodinfo = $cachecoursemodinfo->get_versioned($course->id, $course->cacherev);
|
||||
if (!$coursemodinfo) {
|
||||
$coursemodinfo = self::inner_build_course_cache($course, $lock);
|
||||
}
|
||||
} finally {
|
||||
@@ -681,17 +681,12 @@ class course_modinfo {
|
||||
global $DB, $CFG;
|
||||
require_once("{$CFG->dirroot}/course/lib.php");
|
||||
|
||||
// Ensure object has all necessary fields.
|
||||
foreach (self::$cachedfields as $key) {
|
||||
if (!isset($course->$key)) {
|
||||
$course = $DB->get_record('course', array('id' => $course->id),
|
||||
implode(',', array_merge(array('id'), self::$cachedfields)), MUST_EXIST);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Always reload the course object from database to ensure we have the latest possible
|
||||
// value for cacherev.
|
||||
$course = $DB->get_record('course', ['id' => $course->id],
|
||||
implode(',', array_merge(['id'], self::$cachedfields)), MUST_EXIST);
|
||||
|
||||
// Retrieve all information about activities and sections.
|
||||
// This may take time on large courses and it is possible that another user modifies the same course during this process.
|
||||
// Field cacherev stored in both DB and cache will ensure that cached data matches the current course state.
|
||||
$coursemodinfo = new stdClass();
|
||||
$coursemodinfo->modinfo = get_array_of_activities($course->id);
|
||||
$coursemodinfo->sectioncache = self::build_course_section_cache($course);
|
||||
@@ -700,7 +695,7 @@ class course_modinfo {
|
||||
}
|
||||
// Set the accumulated activities and sections information in cache, together with cacherev.
|
||||
$cachecoursemodinfo = cache::make('core', 'coursemodinfo');
|
||||
$cachecoursemodinfo->set($course->id, $coursemodinfo);
|
||||
$cachecoursemodinfo->set_versioned($course->id, $course->cacherev, $coursemodinfo);
|
||||
return $coursemodinfo;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user