MDL-58136 cache: Add a course completion cache

The last change added 1 db read per course per page which is not OK. Add a cache to compensate.
This commit is contained in:
Damyon Wiese
2017-06-12 16:11:52 +08:00
parent 11127354db
commit 4d26fcd737
5 changed files with 38 additions and 5 deletions
+23 -3
View File
@@ -74,7 +74,16 @@ class completion_completion extends data_object {
* @return data_object instance of data_object or false if none found.
*/
public static function fetch($params) {
return self::fetch_helper('course_completions', __CLASS__, $params);
$cache = cache::make('core', 'coursecompletion');
$key = $params['userid'] . '_' . $params['course'];
if ($hit = $cache->get($key)) {
return $hit['value'];
}
$tocache = self::fetch_helper('course_completions', __CLASS__, $params);
$cache->set($key, ['value' => $tocache]);
return $tocache;
}
/**
@@ -179,9 +188,10 @@ class completion_completion extends data_object {
$this->timeenrolled = 0;
}
$result = false;
// Save record
if ($this->id) {
return $this->update();
$result = $this->update();
} else {
// Make sure reaggregate field is not null
if (!$this->reaggregate) {
@@ -193,7 +203,17 @@ class completion_completion extends data_object {
$this->timestarted = 0;
}
return $this->insert();
$result = $this->insert();
}
if ($result) {
// Update the cached record.
$cache = cache::make('core', 'coursecompletion');
$data = $this->get_record_data();
$key = $data->userid . '_' . $data->course;
$cache->set($key, ['value' => $data]);
}
return $result;
}
}
+2 -1
View File
@@ -41,8 +41,9 @@ $string['cachedef_capabilities'] = 'System capabilities list';
$string['cachedef_config'] = 'Config settings';
$string['cachedef_coursecat'] = 'Course categories lists for particular user';
$string['cachedef_coursecatrecords'] = 'Course categories records';
$string['cachedef_coursecontacts'] = 'List of course contacts';
$string['cachedef_coursecattree'] = 'Course categories tree';
$string['cachedef_coursecompletion'] = 'Course completion status';
$string['cachedef_coursecontacts'] = 'List of course contacts';
$string['cachedef_coursemodinfo'] = 'Accumulated information about modules and sections for each course';
$string['cachedef_completion'] = 'Activity completion status';
$string['cachedef_databasemeta'] = 'Database meta information';
+2
View File
@@ -769,6 +769,7 @@ class completion_info {
// Difficult to find affected users, just purge all completion cache.
cache::make('core', 'completion')->purge();
cache::make('core', 'coursecompletion')->purge();
}
/**
@@ -820,6 +821,7 @@ class completion_info {
// Difficult to find affected users, just purge all completion cache.
cache::make('core', 'completion')->purge();
cache::make('core', 'coursecompletion')->purge();
}
/**
+10
View File
@@ -229,6 +229,16 @@ $definitions = array(
'staticaccelerationsize' => 2, // Should be current course and site course.
),
// Used to cache course completion status.
'coursecompletion' => array(
'mode' => cache_store::MODE_APPLICATION,
'simplekeys' => true,
'simpledata' => true,
'ttl' => 3600,
'staticacceleration' => true,
'staticaccelerationsize' => 30, // Will be users list of current courses in nav.
),
// A simple cache that stores whether a user can expand a course in the navigation.
// The key is the course ID and the value will either be 1 or 0 (cast to bool).
// The cache isn't always up to date, it should only ever be used to save a costly call to
+1 -1
View File
@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2016120503.07; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2016120503.08; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.