From 4d26fcd73702c9f3813224ef9c112ee6971e0424 Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Tue, 6 Jun 2017 13:57:16 +0800 Subject: [PATCH] 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. --- completion/completion_completion.php | 26 +++++++++++++++++++++++--- lang/en/cache.php | 3 ++- lib/completionlib.php | 2 ++ lib/db/caches.php | 10 ++++++++++ version.php | 2 +- 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/completion/completion_completion.php b/completion/completion_completion.php index 4e0a33c4436..04b05c4abf7 100644 --- a/completion/completion_completion.php +++ b/completion/completion_completion.php @@ -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; } } diff --git a/lang/en/cache.php b/lang/en/cache.php index b97ae2b189e..84bcf617947 100644 --- a/lang/en/cache.php +++ b/lang/en/cache.php @@ -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'; diff --git a/lib/completionlib.php b/lib/completionlib.php index 80c854242db..c32ad4c4bfe 100644 --- a/lib/completionlib.php +++ b/lib/completionlib.php @@ -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(); } /** diff --git a/lib/db/caches.php b/lib/db/caches.php index 9a8e9f24e5d..e75a61d4922 100644 --- a/lib/db/caches.php +++ b/lib/db/caches.php @@ -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 diff --git a/version.php b/version.php index 2ac0f1afee1..171ef5071de 100644 --- a/version.php +++ b/version.php @@ -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.