mirror of
https://github.com/moodle/moodle.git
synced 2025-01-19 06:18:28 +01:00
MDL-67585 core_course: add caching_content_item_repository class
A repository object which caches results for a user and course. This allows this repository to be called many time in a single request - a pattern we expect when objects of this type replace the existing get_module_metadata() method.
This commit is contained in:
parent
92715acd0c
commit
7c7ae1bd6f
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://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/>.
|
||||
|
||||
/**
|
||||
* Contains class caching_content_item_repository, for fetching content_items, with additional caching.
|
||||
*
|
||||
* @package core
|
||||
* @subpackage course
|
||||
* @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
namespace core_course\local\repository;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* The class caching_content_item_repository, for fetching content_items, with additional caching.
|
||||
*
|
||||
* This class decorates the content_item_repository and uses the supplied cache to store content items for user and course
|
||||
* combinations. The content items for subsequent calls are returned from the cache if present, else are retrieved from the wrapped
|
||||
* content_item_repository.
|
||||
*
|
||||
* @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class caching_content_item_readonly_repository implements content_item_readonly_repository_interface {
|
||||
|
||||
/** @var \cache $cachestore the cache to use. */
|
||||
private $cachestore;
|
||||
|
||||
/** @var content_item_readonly_repository $contentitemrepository a content item repository. */
|
||||
private $contentitemrepository;
|
||||
|
||||
/**
|
||||
* The caching_content_item_readonly_repository constructor.
|
||||
*
|
||||
* @param \cache $cachestore a cache to use.
|
||||
* @param content_item_readonly_repository $contentitemrepository the repository to use as a fallback, after a cache miss.
|
||||
*/
|
||||
public function __construct(\cache $cachestore, content_item_readonly_repository $contentitemrepository) {
|
||||
$this->cachestore = $cachestore;
|
||||
$this->contentitemrepository = $contentitemrepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all the content items for a given course and user.
|
||||
*
|
||||
* @param \stdClass $course The course to find content items for.
|
||||
* @param \stdClass $user the user to pass to plugins.
|
||||
* @return array the array of content items.
|
||||
*/
|
||||
public function find_all_for_course(\stdClass $course, \stdClass $user): array {
|
||||
global $USER;
|
||||
// Try to find this data in the cache first.
|
||||
$key = $USER->id . '_' . $course->id;
|
||||
$contentitems = $this->cachestore->get($key);
|
||||
if ($contentitems !== false) {
|
||||
return $contentitems;
|
||||
}
|
||||
|
||||
// If we can't find it there, we must get it from the slow data store, updating the cache in the process.
|
||||
$contentitems = $this->contentitemrepository->find_all_for_course($course, $user);
|
||||
$this->cachestore->set($key, $contentitems);
|
||||
return $contentitems;
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://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/>.
|
||||
|
||||
/**
|
||||
* Contains the test class for the caching_content_item_readonly_repository class.
|
||||
*
|
||||
* @package core
|
||||
* @subpackage course
|
||||
* @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
namespace tests\core_course;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
use core_course\local\repository\content_item_readonly_repository;
|
||||
use core_course\local\repository\caching_content_item_readonly_repository;
|
||||
|
||||
/**
|
||||
* The test class for the caching_content_item_readonly_repository class.
|
||||
*
|
||||
* @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class caching_content_item_readonly_repository_testcase extends \advanced_testcase {
|
||||
/**
|
||||
* Test verifying that content items are cached and returned from the cache in subsequent same-request calls.
|
||||
*/
|
||||
public function test_find_all_for_course() {
|
||||
$this->resetAfterTest();
|
||||
global $DB;
|
||||
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
|
||||
$cir = new content_item_readonly_repository();
|
||||
$ccir = new caching_content_item_readonly_repository(\cache::make('core', 'user_course_content_items'), $cir);
|
||||
|
||||
// Get the content items using both the live and the caching repos.
|
||||
$items = $cir->find_all_for_course($course, $user);
|
||||
$cacheditems = $ccir->find_all_for_course($course, $user);
|
||||
$itemsfiltered = array_filter($items, function($item) {
|
||||
return $item->get_component_name() == 'mod_assign';
|
||||
});
|
||||
$cacheditemsfiltered = array_filter($cacheditems, function($item) {
|
||||
return $item->get_component_name() == 'mod_assign';
|
||||
});
|
||||
|
||||
// Verify the assign module is in both result sets.
|
||||
$module = $DB->get_record('modules', ['name' => 'assign']);
|
||||
$this->assertEquals($module->name, $itemsfiltered[0]->get_name());
|
||||
$this->assertEquals($module->name, $cacheditemsfiltered[0]->get_name());
|
||||
|
||||
// Hide a module and get the content items again.
|
||||
$DB->set_field("modules", "visible", "0", ["id" => $module->id]);
|
||||
$items = $cir->find_all_for_course($course, $user);
|
||||
$cacheditems = $ccir->find_all_for_course($course, $user);
|
||||
$itemsfiltered = array_filter($items, function($item) {
|
||||
return $item->get_component_name() == 'mod_assign';
|
||||
});
|
||||
$cacheditemsfiltered = array_filter($cacheditems, function($item) {
|
||||
return $item->get_component_name() == 'mod_assign';
|
||||
});
|
||||
|
||||
// The caching repo should return the same list, while the live repo will return the updated list.
|
||||
$this->assertEquals($module->name, $cacheditemsfiltered[0]->get_name());
|
||||
$this->assertEmpty($itemsfiltered);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user