protected static function inner_build_course_cache($course, \core\lock\lock $lock) {
        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;
            }
        }
        // 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);
        foreach (self::$cachedfields as $key) {
            $coursemodinfo->$key = $course->$key;
        }
        // Set the accumulated activities and sections information in cache, together with cacherev.
        $cachecoursemodinfo = cache::make('core', 'coursemodinfo');
        $cachecoursemodinfo->set($course->id, $coursemodinfo);
        return $coursemodinfo;
    }