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 402 and 403]

   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 API interfaces
  19   *
  20   * This file is part of Moodle's cache API, affectionately called MUC.
  21   * It contains the components that are requried in order to use caching.
  22   *
  23   * @package    core
  24   * @category   cache
  25   * @copyright  2012 Sam Hemelryk
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  /**
  32   * Cache Loader.
  33   *
  34   * This cache loader interface provides the required structure for classes that wish to be interacted with as a
  35   * means of accessing and interacting with a cache.
  36   *
  37   * Can be implemented by any class wishing to be a cache loader.
  38   */
  39  interface cache_loader {
  40  
  41      /**
  42       * Retrieves the value for the given key from the cache.
  43       *
  44       * @param string|int $key The key for the data being requested.
  45       * @param int $strictness One of IGNORE_MISSING or MUST_EXIST.
  46       * @return mixed The data retrieved from the cache, or false if the key did not exist within the cache.
  47       *      If MUST_EXIST was used then an exception will be thrown if the key does not exist within the cache.
  48       */
  49      public function get($key, $strictness = IGNORE_MISSING);
  50  
  51      /**
  52       * Retrieves the value and actual version for the given key, with at least the required version.
  53       *
  54       * If there is no value for the key, or there is a value but it doesn't have the required
  55       * version, then this function will return false (or throw an exception if you set strictness
  56       * to MUST_EXIST).
  57       *
  58       * This function can be used to make it easier to support localisable caches (where the cache
  59       * could be stored on a local server as well as a shared cache). Specifying the version means
  60       * that it will automatically retrieve the correct version if available, either from the local
  61       * server or [if that has an older version] from the shared server.
  62       *
  63       * If the cached version is newer than specified version, it will be returned regardless. For
  64       * example, if you request version 4, but the locally cached version is 5, it will be returned.
  65       * If you request version 6, and the locally cached version is 5, then the system will look in
  66       * higher-level caches (if any); if there still isn't a version 6 or greater, it will return
  67       * null.
  68       *
  69       * You must use this function if you use set_versioned.
  70       *
  71       * @param string|int $key The key for the data being requested.
  72       * @param int $requiredversion Minimum required version of the data
  73       * @param int $strictness One of IGNORE_MISSING or MUST_EXIST.
  74       * @param mixed $actualversion If specified, will be set to the actual version number retrieved
  75       * @return mixed Data from the cache, or false if the key did not exist or was too old
  76       */
  77      public function get_versioned($key, int $requiredversion, int $strictness = IGNORE_MISSING, &$actualversion = null);
  78  
  79      /**
  80       * Retrieves an array of values for an array of keys.
  81       *
  82       * Using this function comes with potential performance implications.
  83       * Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call
  84       * the equivalent singular method for each item provided.
  85       * This should not deter you from using this function as there is a performance benefit in situations where the cache
  86       * store does support it, but you should be aware of this fact.
  87       *
  88       * @param array $keys The keys of the data being requested.
  89       * @param int $strictness One of IGNORE_MISSING or MUST_EXIST.
  90       * @return array An array of key value pairs for the items that could be retrieved from the cache.
  91       *      If MUST_EXIST was used and not all keys existed within the cache then an exception will be thrown.
  92       *      Otherwise any key that did not exist will have a data value of false within the results.
  93       */
  94      public function get_many(array $keys, $strictness = IGNORE_MISSING);
  95  
  96      /**
  97       * Sends a key => value pair to the cache.
  98       *
  99       * <code>
 100       * // This code will add four entries to the cache, one for each url.
 101       * $cache->set('main', 'http://moodle.org');
 102       * $cache->set('docs', 'http://docs.moodle.org');
 103       * $cache->set('tracker', 'http://tracker.moodle.org');
 104       * $cache->set('qa', 'http://qa.moodle.net');
 105       * </code>
 106       *
 107       * @param string|int $key The key for the data being requested.
 108       * @param mixed $data The data to set against the key.
 109       * @return bool True on success, false otherwise.
 110       */
 111      public function set($key, $data);
 112  
 113      /**
 114       * Sets the value for the given key with the given version.
 115       *
 116       * The cache does not store multiple versions - any existing version will be overwritten with
 117       * this one. This function should only be used if there is a known 'current version' (e.g.
 118       * stored in a database table). It only ensures that the cache does not return outdated data.
 119       *
 120       * This function can be used to help implement localisable caches (where the cache could be
 121       * stored on a local server as well as a shared cache). The version will be recorded alongside
 122       * the item and get_versioned will always return the correct version.
 123       *
 124       * The version number must be an integer that always increases. This could be based on the
 125       * current time, or a stored value that increases by 1 each time it changes, etc.
 126       *
 127       * If you use this function you must use get_versioned to retrieve the data.
 128       *
 129       * @param string|int $key The key for the data being set.
 130       * @param int $version Integer for the version of the data
 131       * @param mixed $data The data to set against the key.
 132       * @return bool True on success, false otherwise.
 133       */
 134      public function set_versioned($key, int $version, $data): bool;
 135  
 136      /**
 137       * Sends several key => value pairs to the cache.
 138       *
 139       * Using this function comes with potential performance implications.
 140       * Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call
 141       * the equivalent singular method for each item provided.
 142       * This should not deter you from using this function as there is a performance benefit in situations where the cache store
 143       * does support it, but you should be aware of this fact.
 144       *
 145       * <code>
 146       * // This code will add four entries to the cache, one for each url.
 147       * $cache->set_many(array(
 148       *     'main' => 'http://moodle.org',
 149       *     'docs' => 'http://docs.moodle.org',
 150       *     'tracker' => 'http://tracker.moodle.org',
 151       *     'qa' => ''http://qa.moodle.net'
 152       * ));
 153       * </code>
 154       *
 155       * @param array $keyvaluearray An array of key => value pairs to send to the cache.
 156       * @return int The number of items successfully set. It is up to the developer to check this matches the number of items.
 157       *      ... if they care that is.
 158       */
 159      public function set_many(array $keyvaluearray);
 160  
 161      /**
 162       * Test is a cache has a key.
 163       *
 164       * The use of the has methods is strongly discouraged. In a high load environment the cache may well change between the
 165       * test and any subsequent action (get, set, delete etc).
 166       * Instead it is recommended to write your code in such a way they it performs the following steps:
 167       * <ol>
 168       * <li>Attempt to retrieve the information.</li>
 169       * <li>Generate the information.</li>
 170       * <li>Attempt to set the information</li>
 171       * </ol>
 172       *
 173       * Its also worth mentioning that not all stores support key tests.
 174       * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
 175       * Just one more reason you should not use these methods unless you have a very good reason to do so.
 176       *
 177       * @param string|int $key
 178       * @return bool True if the cache has the requested key, false otherwise.
 179       */
 180      public function has($key);
 181  
 182      /**
 183       * Test if a cache has at least one of the given keys.
 184       *
 185       * It is strongly recommended to avoid the use of this function if not absolutely required.
 186       * In a high load environment the cache may well change between the test and any subsequent action (get, set, delete etc).
 187       *
 188       * Its also worth mentioning that not all stores support key tests.
 189       * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
 190       * Just one more reason you should not use these methods unless you have a very good reason to do so.
 191       *
 192       * @param array $keys
 193       * @return bool True if the cache has at least one of the given keys
 194       */
 195      public function has_any(array $keys);
 196  
 197      /**
 198       * Test is a cache has all of the given keys.
 199       *
 200       * It is strongly recommended to avoid the use of this function if not absolutely required.
 201       * In a high load environment the cache may well change between the test and any subsequent action (get, set, delete etc).
 202       *
 203       * Its also worth mentioning that not all stores support key tests.
 204       * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
 205       * Just one more reason you should not use these methods unless you have a very good reason to do so.
 206       *
 207       * @param array $keys
 208       * @return bool True if the cache has all of the given keys, false otherwise.
 209       */
 210      public function has_all(array $keys);
 211  
 212      /**
 213       * Delete the given key from the cache.
 214       *
 215       * @param string|int $key The key to delete.
 216       * @param bool $recurse When set to true the key will also be deleted from all stacked cache loaders and their stores.
 217       *     This happens by default and ensure that all the caches are consistent. It is NOT recommended to change this.
 218       * @return bool True of success, false otherwise.
 219       */
 220      public function delete($key, $recurse = true);
 221  
 222      /**
 223       * Delete all of the given keys from the cache.
 224       *
 225       * @param array $keys The key to delete.
 226       * @param bool $recurse When set to true the key will also be deleted from all stacked cache loaders and their stores.
 227       *     This happens by default and ensure that all the caches are consistent. It is NOT recommended to change this.
 228       * @return int The number of items successfully deleted.
 229       */
 230      public function delete_many(array $keys, $recurse = true);
 231  }
 232  
 233  /**
 234   * Cache Loader supporting locking.
 235   *
 236   * This interface should be given to classes already implementing cache_loader that also wish to support locking.
 237   * It outlines the required structure for utilising locking functionality when using a cache.
 238   *
 239   * Can be implemented by any class already implementing the cache_loader interface.
 240   */
 241  interface cache_loader_with_locking {
 242  
 243      /**
 244       * Acquires a lock for the given key.
 245       *
 246       * Please note that this happens automatically if the cache definition requires locking.
 247       * it is still made a public method so that adhoc caches can use it if they choose.
 248       * However this doesn't guarantee consistent access. It will become the responsibility of the calling code to ensure
 249       * locks are acquired, checked, and released.
 250       *
 251       * @param string|int $key
 252       * @return bool True if the lock could be acquired, false otherwise.
 253       */
 254      public function acquire_lock($key);
 255  
 256      /**
 257       * Checks if the cache loader owns the lock for the given key.
 258       *
 259       * Please note that this happens automatically if the cache definition requires locking.
 260       * it is still made a public method so that adhoc caches can use it if they choose.
 261       * However this doesn't guarantee consistent access. It will become the responsibility of the calling code to ensure
 262       * locks are acquired, checked, and released.
 263       *
 264       * @param string|int $key
 265       * @return bool True if this code has the lock, false if there is a lock but this code doesn't have it,
 266       *      null if there is no lock.
 267       */
 268      public function check_lock_state($key);
 269  
 270      /**
 271       * Releases the lock for the given key.
 272       *
 273       * Please note that this happens automatically if the cache definition requires locking.
 274       * it is still made a public method so that adhoc caches can use it if they choose.
 275       * However this doesn't guarantee consistent access. It will become the responsibility of the calling code to ensure
 276       * locks are acquired, checked, and released.
 277       *
 278       * @param string|int $key
 279       * @return bool True if the lock has been released, false if there was a problem releasing the lock.
 280       */
 281      public function release_lock($key);
 282  }
 283  
 284  /**
 285   * Cache store feature: locking
 286   *
 287   * This is a feature that cache stores can implement if they wish to support locking themselves rather
 288   * than having the cache loader handle it for them.
 289   *
 290   * Can be implemented by classes already implementing cache_store.
 291   */
 292  interface cache_is_lockable {
 293  
 294      /**
 295       * Acquires a lock on the given key for the given identifier.
 296       *
 297       * @param string $key The key we are locking.
 298       * @param string $ownerid The identifier so we can check if we have the lock or if it is someone else.
 299       *      The use of this property is entirely optional and implementations can act as they like upon it.
 300       * @return bool True if the lock could be acquired, false otherwise.
 301       */
 302      public function acquire_lock($key, $ownerid);
 303  
 304      /**
 305       * Test if there is already a lock for the given key and if there is whether it belongs to the calling code.
 306       *
 307       * @param string $key The key we are locking.
 308       * @param string $ownerid The identifier so we can check if we have the lock or if it is someone else.
 309       * @return bool True if this code has the lock, false if there is a lock but this code doesn't have it, null if there
 310       *      is no lock.
 311       */
 312      public function check_lock_state($key, $ownerid);
 313  
 314      /**
 315       * Releases the lock on the given key.
 316       *
 317       * @param string $key The key we are locking.
 318       * @param string $ownerid The identifier so we can check if we have the lock or if it is someone else.
 319       *      The use of this property is entirely optional and implementations can act as they like upon it.
 320       * @return bool True if the lock has been released, false if there was a problem releasing the lock.
 321       */
 322      public function release_lock($key, $ownerid);
 323  }
 324  
 325  /**
 326   * Cache store feature: key awareness.
 327   *
 328   * This is a feature that cache stores and cache loaders can both choose to implement.
 329   * If a cache store implements this then it will be made responsible for tests for items within the cache.
 330   * If the cache store being used doesn't implement this then it will be the responsibility of the cache loader to use the
 331   * equivalent get methods to mimick the functionality of these tests.
 332   *
 333   * Cache stores should only override these methods if they natively support such features or if they have a better performing
 334   * means of performing these tests than the handling that would otherwise take place in the cache_loader.
 335   *
 336   * Can be implemented by classes already implementing cache_store.
 337   */
 338  interface cache_is_key_aware {
 339  
 340      /**
 341       * Test is a cache has a key.
 342       *
 343       * The use of the has methods is strongly discouraged. In a high load environment the cache may well change between the
 344       * test and any subsequent action (get, set, delete etc).
 345       * Instead it is recommended to write your code in such a way they it performs the following steps:
 346       * <ol>
 347       * <li>Attempt to retrieve the information.</li>
 348       * <li>Generate the information.</li>
 349       * <li>Attempt to set the information</li>
 350       * </ol>
 351       *
 352       * Its also worth mentioning that not all stores support key tests.
 353       * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
 354       * Just one more reason you should not use these methods unless you have a very good reason to do so.
 355       *
 356       * @param string|int $key
 357       * @return bool True if the cache has the requested key, false otherwise.
 358       */
 359      public function has($key);
 360  
 361      /**
 362       * Test if a cache has at least one of the given keys.
 363       *
 364       * It is strongly recommended to avoid the use of this function if not absolutely required.
 365       * In a high load environment the cache may well change between the test and any subsequent action (get, set, delete etc).
 366       *
 367       * Its also worth mentioning that not all stores support key tests.
 368       * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
 369       * Just one more reason you should not use these methods unless you have a very good reason to do so.
 370       *
 371       * @param array $keys
 372       * @return bool True if the cache has at least one of the given keys
 373       */
 374      public function has_any(array $keys);
 375  
 376      /**
 377       * Test is a cache has all of the given keys.
 378       *
 379       * It is strongly recommended to avoid the use of this function if not absolutely required.
 380       * In a high load environment the cache may well change between the test and any subsequent action (get, set, delete etc).
 381       *
 382       * Its also worth mentioning that not all stores support key tests.
 383       * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
 384       * Just one more reason you should not use these methods unless you have a very good reason to do so.
 385       *
 386       * @param array $keys
 387       * @return bool True if the cache has all of the given keys, false otherwise.
 388       */
 389      public function has_all(array $keys);
 390  }
 391  
 392  /**
 393   * Cache store feature: keys are searchable.
 394   *
 395   * Cache stores can choose to implement this interface.
 396   * In order for a store to be usable as a session cache it must implement this interface.
 397   *
 398   * @since Moodle 2.4.4
 399   */
 400  interface cache_is_searchable {
 401      /**
 402       * Finds all of the keys being used by the cache store.
 403       *
 404       * @return array.
 405       */
 406      public function find_all();
 407  
 408      /**
 409       * Finds all of the keys whose keys start with the given prefix.
 410       *
 411       * @param string $prefix
 412       */
 413      public function find_by_prefix($prefix);
 414  }
 415  
 416  /**
 417   * Cache store feature: configurable.
 418   *
 419   * This feature should be implemented by all cache stores that are configurable when adding an instance.
 420   * It requires the implementation of methods required to convert form data into the a configuration array for the
 421   * store instance, and then the reverse converting configuration data into an array that can be used to set the
 422   * data for the edit form.
 423   *
 424   * Can be implemented by classes already implementing cache_store.
 425   */
 426  interface cache_is_configurable {
 427  
 428      /**
 429       * Given the data from the add instance form this function creates a configuration array.
 430       *
 431       * @param stdClass $data
 432       * @return array
 433       */
 434      public static function config_get_configuration_array($data);
 435  
 436      /**
 437       * Allows the cache store to set its data against the edit form before it is shown to the user.
 438       *
 439       * @param moodleform $editform
 440       * @param array $config
 441       */
 442      public static function config_set_edit_form_data(moodleform $editform, array $config);
 443  }
 444  
 445  /**
 446   * Cache Data Source.
 447   *
 448   * The cache data source interface can be implemented by any class within Moodle.
 449   * If implemented then the class can be reference in a cache definition and will be used to load information that cannot be
 450   * retrieved from the cache. As part of its retrieval that information will also be loaded into the cache.
 451   *
 452   * This allows developers to created a complete cache solution that can be used through code ensuring consistent cache
 453   * interaction and loading. Allowing them in turn to centralise code and help keeps things more easily maintainable.
 454   *
 455   * Can be implemented by any class.
 456   *
 457   * @package    core
 458   * @category   cache
 459   * @copyright  2012 Sam Hemelryk
 460   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 461   */
 462  interface cache_data_source {
 463  
 464      /**
 465       * Returns an instance of the data source class that the cache can use for loading data using the other methods
 466       * specified by this interface.
 467       *
 468       * @param cache_definition $definition
 469       * @return object
 470       */
 471      public static function get_instance_for_cache(cache_definition $definition);
 472  
 473      /**
 474       * Loads the data for the key provided ready formatted for caching.
 475       *
 476       * @param string|int $key The key to load.
 477       * @return mixed What ever data should be returned, or false if it can't be loaded.
 478       */
 479      public function load_for_cache($key);
 480  
 481      /**
 482       * Loads several keys for the cache.
 483       *
 484       * @param array $keys An array of keys each of which will be string|int.
 485       * @return array An array of matching data items.
 486       */
 487      public function load_many_for_cache(array $keys);
 488  }
 489  
 490  /**
 491   * Versionable cache data source.
 492   *
 493   * This interface extends the main cache data source interface to add an extra required method if
 494   * the data source is to be used for a versioned cache.
 495   *
 496   * @package core_cache
 497   */
 498  interface cache_data_source_versionable extends cache_data_source {
 499      /**
 500       * Loads the data for the key provided ready formatted for caching.
 501       *
 502       * If there is no data for that key, or if the data for the required key has an older version
 503       * than the specified $requiredversion, then this returns null.
 504       *
 505       * If there is data then $actualversion should be set to the actual version number retrieved
 506       * (may be the same as $requiredversion or newer).
 507       *
 508       * @param string|int $key The key to load.
 509       * @param int $requiredversion Minimum required version
 510       * @param mixed $actualversion Should be set to the actual version number retrieved
 511       * @return mixed What ever data should be returned, or false if it can't be loaded.
 512       */
 513      public function load_for_cache_versioned($key, int $requiredversion, &$actualversion);
 514  }
 515  
 516  /**
 517   * Cacheable object.
 518   *
 519   * This interface can be implemented by any class that is going to be passed into a cache and allows it to take control of the
 520   * structure and the information about to be cached, as well as how to deal with it when it is retrieved from a cache.
 521   * Think of it like serialisation and the __sleep and __wakeup methods.
 522   * This is used because cache stores are responsible for how they interact with data and what they do when storing it. This
 523   * interface ensures there is always a guaranteed action.
 524   */
 525  interface cacheable_object {
 526  
 527      /**
 528       * Prepares the object for caching. Works like the __sleep method.
 529       *
 530       * @return mixed The data to cache, can be anything except a class that implements the cacheable_object... that would
 531       *      be dumb.
 532       */
 533      public function prepare_to_cache();
 534  
 535      /**
 536       * Takes the data provided by prepare_to_cache and reinitialises an instance of the associated from it.
 537       *
 538       * @param mixed $data
 539       * @return object The instance for the given data.
 540       */
 541      public static function wake_from_cache($data);
 542  }
 543  
 544  /**
 545   * Cache lock interface
 546   *
 547   * This interface needs to be inherited by all cache lock plugins.
 548   */
 549  interface cache_lock_interface {
 550      /**
 551       * Constructs an instance of the cache lock given its name and its configuration data
 552       *
 553       * @param string $name The unique name of the lock instance
 554       * @param array $configuration
 555       */
 556      public function __construct($name, array $configuration = array());
 557  
 558      /**
 559       * Acquires a lock on a given key.
 560       *
 561       * @param string $key The key to acquire a lock for.
 562       * @param string $ownerid An unique identifier for the owner of this lock. It is entirely optional for the cache lock plugin
 563       *      to use this. Each implementation can decide for themselves.
 564       * @param bool $block If set to true the application will wait until a lock can be acquired
 565       * @return bool True if the lock can be acquired false otherwise.
 566       */
 567      public function lock($key, $ownerid, $block = false);
 568  
 569      /**
 570       * Releases the lock held on a certain key.
 571       *
 572       * @param string $key The key to release the lock for.
 573       * @param string $ownerid An unique identifier for the owner of this lock. It is entirely optional for the cache lock plugin
 574       *      to use this. Each implementation can decide for themselves.
 575       * @param bool $forceunlock If set to true the lock will be removed if it exists regardless of whether or not we own it.
 576       */
 577      public function unlock($key, $ownerid, $forceunlock = false);
 578  
 579      /**
 580       * Checks the state of the given key.
 581       *
 582       * Returns true if the key is locked and belongs to the ownerid.
 583       * Returns false if the key is locked but does not belong to the ownerid.
 584       * Returns null if there is no lock
 585       *
 586       * @param string $key The key we are checking for.
 587       * @param string $ownerid The identifier so we can check if we have the lock or if it is someone else.
 588       * @return bool True if this code has the lock, false if there is a lock but this code doesn't have it, null if there
 589       *      is no lock.
 590       */
 591      public function check_state($key, $ownerid);
 592  
 593      /**
 594       * Cleans up any left over locks.
 595       *
 596       * This function MUST clean up any locks that have been acquired and not released during processing.
 597       * Although the situation of acquiring a lock and not releasing it should be insanely rare we need to deal with it.
 598       * Things such as unfortunate timeouts etc could cause this situation.
 599       */
 600      public function __destruct();
 601  }