MDL-64820 forum: add caching to forum_tp_is_tracked

This commit is contained in:
Ryan Wyllie 2019-03-08 15:02:06 +08:00
parent 72a3d05b7e
commit 4dc671a920
4 changed files with 56 additions and 2 deletions

35
mod/forum/db/caches.php Normal file
View File

@ -0,0 +1,35 @@
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Defined caches used internally by the plugin.
*
* @package mod_forum
* @category cache
* @copyright 2019 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$definitions = [
'forum_is_tracked' => [
'mode' => cache_store::MODE_REQUEST,
'simplekeys' => true,
'simpledata' => true,
'staticacceleration' => true
],
];

View File

@ -50,6 +50,7 @@ $string['blockperiod_help'] = 'Students can be blocked from posting more than a
$string['blockperioddisabled'] = 'Don\'t block';
$string['blogforum'] = 'Standard forum displayed in a blog-like format';
$string['bynameondate'] = 'by {$a->name} - {$a->date}';
$string['cachedef_forum_is_tracked'] = 'Forum tracking status for user';
$string['cannotadd'] = 'Could not add the discussion for this forum';
$string['cannotadddiscussion'] = 'Adding discussions to this forum requires group membership.';
$string['cannotadddiscussionall'] = 'You do not have permission to add a new discussion topic for all participants.';

View File

@ -5970,6 +5970,13 @@ function forum_tp_is_tracked($forum, $user=false) {
return false;
}
$cache = cache::make('mod_forum', 'forum_is_tracked');
$forumid = is_numeric($forum) ? $forum : $forum->id;
$key = $forumid . '_' . $user->id;
if ($cachedvalue = $cache->get($key)) {
return $cachedvalue == 'tracked';
}
// Work toward always passing an object...
if (is_numeric($forum)) {
debugging('Better use proper forum object.', DEBUG_DEVELOPER);
@ -5985,10 +5992,17 @@ function forum_tp_is_tracked($forum, $user=false) {
$userpref = $DB->get_record('forum_track_prefs', array('userid' => $user->id, 'forumid' => $forum->id));
if ($CFG->forum_allowforcedreadtracking) {
return $forumforced || ($forumallows && $userpref === false);
$istracked = $forumforced || ($forumallows && $userpref === false);
} else {
return ($forumallows || $forumforced) && $userpref === false;
$istracked = ($forumallows || $forumforced) && $userpref === false;
}
// We have to store a string here because the cache API returns false
// when it can't find the key which would be confused with our legitimate
// false value. *sigh*.
$cache->set($key, $istracked ? 'tracked' : 'not');
return $istracked;
}
/**

View File

@ -271,6 +271,7 @@ class mod_forum_lib_testcase extends advanced_testcase {
$this->resetAfterTest();
$cache = cache::make('mod_forum', 'forum_is_tracked');
$useron = $this->getDataGenerator()->create_user(array('trackforums' => 1));
$useroff = $this->getDataGenerator()->create_user(array('trackforums' => 0));
$course = $this->getDataGenerator()->create_course();
@ -310,6 +311,7 @@ class mod_forum_lib_testcase extends advanced_testcase {
$result = forum_tp_is_tracked($forumoptional, $useroff);
$this->assertEquals(false, $result);
$cache->purge();
// Don't allow force.
$CFG->forum_allowforcedreadtracking = 0;
@ -343,6 +345,7 @@ class mod_forum_lib_testcase extends advanced_testcase {
forum_tp_stop_tracking($forumforce->id, $useroff->id);
forum_tp_stop_tracking($forumoptional->id, $useroff->id);
$cache->purge();
// Allow force.
$CFG->forum_allowforcedreadtracking = 1;
@ -362,6 +365,7 @@ class mod_forum_lib_testcase extends advanced_testcase {
$result = forum_tp_is_tracked($forumoptional, $useroff);
$this->assertEquals(false, $result);
$cache->purge();
// Don't allow force.
$CFG->forum_allowforcedreadtracking = 0;