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:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user