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;
}
}