MDL-66220 mod_forum: Cache result of forum_user_can_see_post()

* Cache the result of this function to avoid doing multiple DB calls
for the same post when loading posts in a discussion.
This commit is contained in:
Jun Pataleta 2020-01-13 13:09:14 +08:00
parent bf7a6ff4d7
commit 49801df749

View File

@ -61,6 +61,8 @@ class capability {
private $forumrecord;
/** @var context $context Module context for the forum */
private $context;
/** @var array $canviewpostcache Cache of discussion posts that can be viewed by a user. */
protected $canviewpostcache = [];
/**
* Constructor.
@ -361,12 +363,23 @@ class capability {
return false;
}
// Return cached can view if possible.
if (isset($this->canviewpostcache[$user->id][$post->get_id()])) {
return $this->canviewpostcache[$user->id][$post->get_id()];
}
// Otherwise, check if the user can see this post.
$forum = $this->get_forum();
$forumrecord = $this->get_forum_record();
$discussionrecord = $this->get_discussion_record($discussion);
$postrecord = $this->get_post_record($post);
$coursemodule = $forum->get_course_module_record();
return forum_user_can_see_post($forumrecord, $discussionrecord, $postrecord, $user, $coursemodule, false);
$canviewpost = forum_user_can_see_post($forumrecord, $discussionrecord, $postrecord, $user, $coursemodule, false);
// Then cache the result before returning.
$this->canviewpostcache[$user->id][$post->get_id()] = $canviewpost;
return $canviewpost;
}
/**