Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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   * Cache dummy store.
  19   *
  20   * This dummy store is used when a load has no other stores that it can make use of.
  21   * This shouldn't happen in normal operation... I think.
  22   *
  23   * This file is part of Moodle's cache API, affectionately called MUC.
  24   * It contains the components that are requried in order to use caching.
  25   *
  26   * @package    core
  27   * @category   cache
  28   * @copyright  2012 Sam Hemelryk
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  
  32  defined('MOODLE_INTERNAL') || die();
  33  
  34  /**
  35   * The cache dummy store.
  36   *
  37   * @copyright  2012 Sam Hemelryk
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class cachestore_dummy extends cache_store {
  41  
  42      /**
  43       * The name of this store.
  44       * @var string
  45       */
  46      protected $name;
  47  
  48      /**
  49       * Gets set to true if this store is going to store data.
  50       * This happens when the definition doesn't require static acceleration as the loader will not be storing information and
  51       * something has to.
  52       * @var bool
  53       */
  54      protected $persist = false;
  55  
  56      /**
  57       * The stored data array
  58       * @var array
  59       */
  60      protected $store = array();
  61  
  62      /**
  63       * Cache definition
  64       * @var cache_definition
  65       */
  66      protected $definition;
  67  
  68      /**
  69       * Constructs a dummy store instance.
  70       * @param string $name
  71       * @param array $configuration
  72       */
  73      public function __construct($name = 'Dummy store', array $configuration = array()) {
  74          $this->name = $name;
  75      }
  76  
  77      /**
  78       * Returns true if this store plugin is usable.
  79       * @return bool
  80       */
  81      public static function are_requirements_met() {
  82          return true;
  83      }
  84  
  85      /**
  86       * Returns true if the user can add an instance.
  87       * @return bool
  88       */
  89      public static function can_add_instance() {
  90          return false;
  91      }
  92  
  93      /**
  94       * Returns the supported features.
  95       * @param array $configuration
  96       * @return int
  97       */
  98      public static function get_supported_features(array $configuration = array()) {
  99          return self::SUPPORTS_NATIVE_TTL;
 100      }
 101  
 102      /**
 103       * Returns the supported mode.
 104       * @param array $configuration
 105       * @return int
 106       */
 107      public static function get_supported_modes(array $configuration = array()) {
 108          return self::MODE_APPLICATION + self::MODE_REQUEST + self::MODE_SESSION;
 109      }
 110  
 111      /**
 112       * Initialises the store instance for a definition.
 113       * @param cache_definition $definition
 114       */
 115      public function initialise(cache_definition $definition) {
 116          // If the definition isn't using static acceleration then we need to be store data here.
 117          // The reasoning behind this is that:
 118          //   - If the definition is using static acceleration then the cache loader is going to
 119          //     store things in its static array.
 120          //   - If the definition is not using static acceleration then the cache loader won't try to store anything
 121          //     and we will need to store it here in order to make sure it is accessible.
 122          if ($definition->get_mode() !== self::MODE_APPLICATION) {
 123              // Neither the request cache nor the session cache provide static acceleration.
 124              $this->persist = true;
 125          } else {
 126              $this->persist = !$definition->use_static_acceleration();
 127          }
 128  
 129          $this->definition = $definition;
 130      }
 131  
 132      /**
 133       * Returns true if this has been initialised.
 134       * @return bool
 135       */
 136      public function is_initialised() {
 137          return (!empty($this->definition));
 138      }
 139  
 140      /**
 141       * Returns true the given mode is supported.
 142       * @param int $mode
 143       * @return bool
 144       */
 145      public static function is_supported_mode($mode) {
 146          return true;
 147      }
 148  
 149      /**
 150       * Returns the data for the given key
 151       * @param string $key
 152       * @return string|false
 153       */
 154      public function get($key) {
 155          if ($this->persist && array_key_exists($key, $this->store)) {
 156              return $this->store[$key];
 157          }
 158          return false;
 159      }
 160  
 161      /**
 162       * Gets' the values for many keys
 163       * @param array $keys
 164       * @return bool
 165       */
 166      public function get_many($keys) {
 167          $return = array();
 168          foreach ($keys as $key) {
 169              if ($this->persist && array_key_exists($key, $this->store)) {
 170                  $return[$key] = $this->store[$key];
 171              } else {
 172                  $return[$key] = false;
 173              }
 174          }
 175          return $return;
 176      }
 177  
 178      /**
 179       * Sets an item in the cache
 180       * @param string $key
 181       * @param mixed $data
 182       * @return bool
 183       */
 184      public function set($key, $data) {
 185          if ($this->persist) {
 186              $this->store[$key] = $data;
 187          }
 188          return true;
 189      }
 190  
 191      /**
 192       * Sets many items in the cache
 193       * @param array $keyvaluearray
 194       * @return int
 195       */
 196      public function set_many(array $keyvaluearray) {
 197          if ($this->persist) {
 198              foreach ($keyvaluearray as $pair) {
 199                  $this->store[$pair['key']] = $pair['value'];
 200              }
 201  
 202          }
 203          return count($keyvaluearray);
 204      }
 205  
 206      /**
 207       * Deletes an item from the cache
 208       * @param string $key
 209       * @return bool
 210       */
 211      public function delete($key) {
 212          unset($this->store[$key]);
 213          return true;
 214      }
 215      /**
 216       * Deletes many items from the cache
 217       * @param array $keys
 218       * @return bool
 219       */
 220      public function delete_many(array $keys) {
 221          if ($this->persist) {
 222              foreach ($keys as $key) {
 223                  unset($this->store[$key]);
 224              }
 225          }
 226          return count($keys);
 227      }
 228  
 229      /**
 230       * Deletes all of the items from the cache.
 231       * @return bool
 232       */
 233      public function purge() {
 234          $this->store = array();
 235          return true;
 236      }
 237  
 238      /**
 239       * Performs any necessary clean up when the store instance is being deleted.
 240       *
 241       * @deprecated since 3.2
 242       * @see cachestore_dummy::instance_deleted()
 243       */
 244      public function cleanup() {
 245          debugging('cachestore_dummy::cleanup() is deprecated. Please use cachestore_dummy::instance_deleted() instead.',
 246              DEBUG_DEVELOPER);
 247          $this->instance_deleted();
 248      }
 249  
 250      /**
 251       * Performs any necessary operation when the store instance is being deleted.
 252       *
 253       * This method may be called before the store has been initialised.
 254       *
 255       * @since Moodle 3.2
 256       */
 257      public function instance_deleted() {
 258          $this->purge();
 259      }
 260  
 261      /**
 262       * Generates an instance of the cache store that can be used for testing.
 263       *
 264       * @param cache_definition $definition
 265       * @return false
 266       */
 267      public static function initialise_test_instance(cache_definition $definition) {
 268          $cache = new cachestore_dummy('Dummy store test');
 269          if ($cache->is_ready()) {
 270              $cache->initialise($definition);
 271          }
 272          return $cache;
 273      }
 274  
 275      /**
 276       * Generates the appropriate configuration required for unit testing.
 277       *
 278       * @return array Array of unit test configuration data to be used by initialise().
 279       */
 280      public static function unit_test_configuration() {
 281          return [];
 282      }
 283  
 284      /**
 285       * Returns the name of this instance.
 286       * @return string
 287       */
 288      public function my_name() {
 289          return $this->name;
 290      }
 291  }