Merge branch 'MDL-79667-main' of https://github.com/mihailges/moodle
[moodle.git] / cache / classes / interfaces.php
blobe0dff287f64c4d46c8c5c1eab1e3ef943b8506d4
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.
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/>.
17 /**
18 * Cache API interfaces
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.
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
29 defined('MOODLE_INTERNAL') || die();
31 /**
32 * Cache Loader.
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.
37 * Can be implemented by any class wishing to be a cache loader.
39 interface cache_loader {
41 /**
42 * Retrieves the value for the given key from the cache.
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.
49 public function get($key, $strictness = IGNORE_MISSING);
51 /**
52 * Retrieves the value and actual version for the given key, with at least the required version.
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).
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.
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.
69 * You must use this function if you use set_versioned.
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
77 public function get_versioned($key, int $requiredversion, int $strictness = IGNORE_MISSING, &$actualversion = null);
79 /**
80 * Retrieves an array of values for an array of keys.
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.
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.
94 public function get_many(array $keys, $strictness = IGNORE_MISSING);
96 /**
97 * Sends a key => value pair to the cache.
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>
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.
111 public function set($key, $data);
114 * Sets the value for the given key with the given version.
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.
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.
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.
127 * If you use this function you must use get_versioned to retrieve the data.
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.
134 public function set_versioned($key, int $version, $data): bool;
137 * Sends several key => value pairs to the cache.
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.
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>
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.
159 public function set_many(array $keyvaluearray);
162 * Test is a cache has a key.
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>
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.
177 * @param string|int $key
178 * @return bool True if the cache has the requested key, false otherwise.
180 public function has($key);
183 * Test if a cache has at least one of the given keys.
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).
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.
192 * @param array $keys
193 * @return bool True if the cache has at least one of the given keys
195 public function has_any(array $keys);
198 * Test is a cache has all of the given keys.
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).
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.
207 * @param array $keys
208 * @return bool True if the cache has all of the given keys, false otherwise.
210 public function has_all(array $keys);
213 * Delete the given key from the cache.
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.
220 public function delete($key, $recurse = true);
223 * Delete all of the given keys from the cache.
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.
230 public function delete_many(array $keys, $recurse = true);
234 * Cache Loader supporting locking.
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.
239 * Can be implemented by any class already implementing the cache_loader interface.
241 interface cache_loader_with_locking {
244 * Acquires a lock for the given key.
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.
251 * Prior to Moodle 4,3 this function used to return false if the lock cannot be obtained. It
252 * now always returns true, and throws an exception if the lock cannot be obtained.
254 * @param string|int $key
255 * @return bool Always returns true (for backwards compatibility)
256 * @throws moodle_exception If the lock cannot be obtained after a timeout
258 public function acquire_lock($key);
261 * Checks if the cache loader owns the lock for the given key.
263 * Please note that this happens automatically if the cache definition requires locking.
264 * it is still made a public method so that adhoc caches can use it if they choose.
265 * However this doesn't guarantee consistent access. It will become the responsibility of the calling code to ensure
266 * locks are acquired, checked, and released.
268 * @param string|int $key
269 * @return bool True if this code has the lock, false if there is a lock but this code doesn't have it,
270 * null if there is no lock.
272 public function check_lock_state($key);
275 * Releases the lock for the given key.
277 * Please note that this happens automatically if the cache definition requires locking.
278 * it is still made a public method so that adhoc caches can use it if they choose.
279 * However this doesn't guarantee consistent access. It will become the responsibility of the calling code to ensure
280 * locks are acquired, checked, and released.
282 * @param string|int $key
283 * @return bool True if the lock has been released, false if there was a problem releasing the lock.
285 public function release_lock($key);
289 * Cache store feature: locking
291 * This is a feature that cache stores can implement if they wish to support locking themselves rather
292 * than having the cache loader handle it for them.
294 * Can be implemented by classes already implementing cache_store.
296 interface cache_is_lockable {
299 * Acquires a lock on the given key for the given identifier.
301 * @param string $key The key we are locking.
302 * @param string $ownerid The identifier so we can check if we have the lock or if it is someone else.
303 * The use of this property is entirely optional and implementations can act as they like upon it.
304 * @return bool True if the lock could be acquired, false otherwise.
306 public function acquire_lock($key, $ownerid);
309 * Test if there is already a lock for the given key and if there is whether it belongs to the calling code.
311 * @param string $key The key we are locking.
312 * @param string $ownerid The identifier so we can check if we have the lock or if it is someone else.
313 * @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
314 * is no lock.
316 public function check_lock_state($key, $ownerid);
319 * Releases the lock on the given key.
321 * @param string $key The key we are locking.
322 * @param string $ownerid The identifier so we can check if we have the lock or if it is someone else.
323 * The use of this property is entirely optional and implementations can act as they like upon it.
324 * @return bool True if the lock has been released, false if there was a problem releasing the lock.
326 public function release_lock($key, $ownerid);
330 * Cache store feature: key awareness.
332 * This is a feature that cache stores and cache loaders can both choose to implement.
333 * If a cache store implements this then it will be made responsible for tests for items within the cache.
334 * If the cache store being used doesn't implement this then it will be the responsibility of the cache loader to use the
335 * equivalent get methods to mimick the functionality of these tests.
337 * Cache stores should only override these methods if they natively support such features or if they have a better performing
338 * means of performing these tests than the handling that would otherwise take place in the cache_loader.
340 * Can be implemented by classes already implementing cache_store.
342 interface cache_is_key_aware {
345 * Test is a cache has a key.
347 * The use of the has methods is strongly discouraged. In a high load environment the cache may well change between the
348 * test and any subsequent action (get, set, delete etc).
349 * Instead it is recommended to write your code in such a way they it performs the following steps:
350 * <ol>
351 * <li>Attempt to retrieve the information.</li>
352 * <li>Generate the information.</li>
353 * <li>Attempt to set the information</li>
354 * </ol>
356 * Its also worth mentioning that not all stores support key tests.
357 * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
358 * Just one more reason you should not use these methods unless you have a very good reason to do so.
360 * @param string|int $key
361 * @return bool True if the cache has the requested key, false otherwise.
363 public function has($key);
366 * Test if a cache has at least one of the given keys.
368 * It is strongly recommended to avoid the use of this function if not absolutely required.
369 * In a high load environment the cache may well change between the test and any subsequent action (get, set, delete etc).
371 * Its also worth mentioning that not all stores support key tests.
372 * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
373 * Just one more reason you should not use these methods unless you have a very good reason to do so.
375 * @param array $keys
376 * @return bool True if the cache has at least one of the given keys
378 public function has_any(array $keys);
381 * Test is a cache has all of the given keys.
383 * It is strongly recommended to avoid the use of this function if not absolutely required.
384 * In a high load environment the cache may well change between the test and any subsequent action (get, set, delete etc).
386 * Its also worth mentioning that not all stores support key tests.
387 * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
388 * Just one more reason you should not use these methods unless you have a very good reason to do so.
390 * @param array $keys
391 * @return bool True if the cache has all of the given keys, false otherwise.
393 public function has_all(array $keys);
397 * Cache store feature: keys are searchable.
399 * Cache stores can choose to implement this interface.
400 * In order for a store to be usable as a session cache it must implement this interface.
402 * @since Moodle 2.4.4
404 interface cache_is_searchable {
406 * Finds all of the keys being used by the cache store.
408 * @return array.
410 public function find_all();
413 * Finds all of the keys whose keys start with the given prefix.
415 * @param string $prefix
417 public function find_by_prefix($prefix);
421 * Cache store feature: configurable.
423 * This feature should be implemented by all cache stores that are configurable when adding an instance.
424 * It requires the implementation of methods required to convert form data into the a configuration array for the
425 * store instance, and then the reverse converting configuration data into an array that can be used to set the
426 * data for the edit form.
428 * Can be implemented by classes already implementing cache_store.
430 interface cache_is_configurable {
433 * Given the data from the add instance form this function creates a configuration array.
435 * @param stdClass $data
436 * @return array
438 public static function config_get_configuration_array($data);
441 * Allows the cache store to set its data against the edit form before it is shown to the user.
443 * @param moodleform $editform
444 * @param array $config
446 public static function config_set_edit_form_data(moodleform $editform, array $config);
450 * Cache Data Source.
452 * The cache data source interface can be implemented by any class within Moodle.
453 * If implemented then the class can be reference in a cache definition and will be used to load information that cannot be
454 * retrieved from the cache. As part of its retrieval that information will also be loaded into the cache.
456 * This allows developers to created a complete cache solution that can be used through code ensuring consistent cache
457 * interaction and loading. Allowing them in turn to centralise code and help keeps things more easily maintainable.
459 * Can be implemented by any class.
461 * @package core
462 * @category cache
463 * @copyright 2012 Sam Hemelryk
464 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
466 interface cache_data_source {
469 * Returns an instance of the data source class that the cache can use for loading data using the other methods
470 * specified by this interface.
472 * @param cache_definition $definition
473 * @return object
475 public static function get_instance_for_cache(cache_definition $definition);
478 * Loads the data for the key provided ready formatted for caching.
480 * @param string|int $key The key to load.
481 * @return mixed What ever data should be returned, or false if it can't be loaded.
483 public function load_for_cache($key);
486 * Loads several keys for the cache.
488 * @param array $keys An array of keys each of which will be string|int.
489 * @return array An array of matching data items.
491 public function load_many_for_cache(array $keys);
495 * Versionable cache data source.
497 * This interface extends the main cache data source interface to add an extra required method if
498 * the data source is to be used for a versioned cache.
500 * @package core_cache
502 interface cache_data_source_versionable extends cache_data_source {
504 * Loads the data for the key provided ready formatted for caching.
506 * If there is no data for that key, or if the data for the required key has an older version
507 * than the specified $requiredversion, then this returns null.
509 * If there is data then $actualversion should be set to the actual version number retrieved
510 * (may be the same as $requiredversion or newer).
512 * @param string|int $key The key to load.
513 * @param int $requiredversion Minimum required version
514 * @param mixed $actualversion Should be set to the actual version number retrieved
515 * @return mixed What ever data should be returned, or false if it can't be loaded.
517 public function load_for_cache_versioned($key, int $requiredversion, &$actualversion);
521 * Cacheable object.
523 * 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
524 * structure and the information about to be cached, as well as how to deal with it when it is retrieved from a cache.
525 * Think of it like serialisation and the __sleep and __wakeup methods.
526 * This is used because cache stores are responsible for how they interact with data and what they do when storing it. This
527 * interface ensures there is always a guaranteed action.
529 interface cacheable_object {
532 * Prepares the object for caching. Works like the __sleep method.
534 * @return mixed The data to cache, can be anything except a class that implements the cacheable_object... that would
535 * be dumb.
537 public function prepare_to_cache();
540 * Takes the data provided by prepare_to_cache and reinitialises an instance of the associated from it.
542 * @param mixed $data
543 * @return object The instance for the given data.
545 public static function wake_from_cache($data);
549 * Cache lock interface
551 * This interface needs to be inherited by all cache lock plugins.
553 interface cache_lock_interface {
555 * Constructs an instance of the cache lock given its name and its configuration data
557 * @param string $name The unique name of the lock instance
558 * @param array $configuration
560 public function __construct($name, array $configuration = array());
563 * Acquires a lock on a given key.
565 * @param string $key The key to acquire a lock for.
566 * @param string $ownerid An unique identifier for the owner of this lock. It is entirely optional for the cache lock plugin
567 * to use this. Each implementation can decide for themselves.
568 * @param bool $block If set to true the application will wait until a lock can be acquired
569 * @return bool True if the lock can be acquired false otherwise.
571 public function lock($key, $ownerid, $block = false);
574 * Releases the lock held on a certain key.
576 * @param string $key The key to release the lock for.
577 * @param string $ownerid An unique identifier for the owner of this lock. It is entirely optional for the cache lock plugin
578 * to use this. Each implementation can decide for themselves.
579 * @param bool $forceunlock If set to true the lock will be removed if it exists regardless of whether or not we own it.
581 public function unlock($key, $ownerid, $forceunlock = false);
584 * Checks the state of the given key.
586 * Returns true if the key is locked and belongs to the ownerid.
587 * Returns false if the key is locked but does not belong to the ownerid.
588 * Returns null if there is no lock
590 * @param string $key The key we are checking for.
591 * @param string $ownerid The identifier so we can check if we have the lock or if it is someone else.
592 * @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
593 * is no lock.
595 public function check_state($key, $ownerid);
598 * Cleans up any left over locks.
600 * This function MUST clean up any locks that have been acquired and not released during processing.
601 * Although the situation of acquiring a lock and not releasing it should be insanely rare we need to deal with it.
602 * Things such as unfortunate timeouts etc could cause this situation.
604 public function __destruct();