Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Contains class caching_content_item_repository, for fetching content_items, with additional caching.
  19   *
  20   * @package    core
  21   * @subpackage course
  22   * @copyright  2020 Jake Dallimore <jrhdallimore@gmail.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  namespace core_course\local\repository;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * The class caching_content_item_repository, for fetching content_items, with additional caching.
  31   *
  32   * This class decorates the content_item_repository and uses the supplied cache to store content items for user and course
  33   * combinations. The content items for subsequent calls are returned from the cache if present, else are retrieved from the wrapped
  34   * content_item_repository.
  35   *
  36   * @copyright  2020 Jake Dallimore <jrhdallimore@gmail.com>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class caching_content_item_readonly_repository implements content_item_readonly_repository_interface {
  40  
  41      /** @var \cache $cachestore the cache to use. */
  42      private $cachestore;
  43  
  44      /** @var content_item_readonly_repository $contentitemrepository a content item repository. */
  45      private $contentitemrepository;
  46  
  47      /**
  48       * The caching_content_item_readonly_repository constructor.
  49       *
  50       * @param \cache $cachestore a cache to use.
  51       * @param content_item_readonly_repository $contentitemrepository the repository to use as a fallback, after a cache miss.
  52       */
  53      public function __construct(\cache $cachestore, content_item_readonly_repository $contentitemrepository) {
  54          $this->cachestore = $cachestore;
  55          $this->contentitemrepository = $contentitemrepository;
  56      }
  57  
  58      /**
  59       * Find all the content items for a given course and user.
  60       *
  61       * @param \stdClass $course The course to find content items for.
  62       * @param \stdClass $user the user to pass to plugins.
  63       * @return array the array of content items.
  64       */
  65      public function find_all_for_course(\stdClass $course, \stdClass $user): array {
  66          global $USER;
  67          // Try to find this data in the cache first.
  68          $key = $USER->id . '_' . $course->id;
  69          $contentitems = $this->cachestore->get($key);
  70          if ($contentitems !== false) {
  71              return $contentitems;
  72          }
  73  
  74          // If we can't find it there, we must get it from the slow data store, updating the cache in the process.
  75          $contentitems = $this->contentitemrepository->find_all_for_course($course, $user);
  76          $this->cachestore->set($key, $contentitems);
  77          return $contentitems;
  78      }
  79  
  80      /**
  81       * Find all the content items made available by core and plugins.
  82       *
  83       * @return array
  84       */
  85      public function find_all(): array {
  86          return $this->contentitemrepository->find_all();
  87      }
  88  }