Skip to content

Commit

Permalink
MDL-58136 cache: Add a course completion cache
Browse files Browse the repository at this point in the history
The last change added 1 db read per course per page which is not OK. Add a cache to compensate.
  • Loading branch information
Damyon Wiese committed Jun 12, 2017
1 parent e1d1519 commit 9a9b9e5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
26 changes: 23 additions & 3 deletions completion/completion_completion.php
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -179,9 +188,10 @@ private function _save() {
$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) {
Expand All @@ -193,7 +203,17 @@ private function _save() {
$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;
}
}
3 changes: 2 additions & 1 deletion lang/en/cache.php
Expand Up @@ -41,8 +41,9 @@
$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';
Expand Down
2 changes: 2 additions & 0 deletions lib/completionlib.php
Expand Up @@ -769,6 +769,7 @@ public function delete_course_completion_data() {

// Difficult to find affected users, just purge all completion cache.
cache::make('core', 'completion')->purge();
cache::make('core', 'coursecompletion')->purge();
}

/**
Expand Down Expand Up @@ -820,6 +821,7 @@ public function delete_all_state($cm) {

// Difficult to find affected users, just purge all completion cache.
cache::make('core', 'completion')->purge();
cache::make('core', 'coursecompletion')->purge();
}

/**
Expand Down
10 changes: 10 additions & 0 deletions lib/db/caches.php
Expand Up @@ -229,6 +229,16 @@
'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
Expand Down
2 changes: 1 addition & 1 deletion version.php
Expand Up @@ -29,7 +29,7 @@

defined('MOODLE_INTERNAL') || die();

$version = 2017051500.04; // 20170515 = branching date YYYYMMDD - do not modify!
$version = 2017051500.05; // 20170515 = branching date YYYYMMDD - do not modify!
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.

Expand Down

0 comments on commit 9a9b9e5

Please sign in to comment.