Merge branch 'MDL-62387-38' of https://github.com/paulholden/moodle into MOODLE_38_STABLE
[moodle.git] / cache / classes / store.php
blob4fcb03ff20bfbaa46450d8ee73e8a405e48c5d7c
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 store - base class
20 * This file is part of Moodle's cache API, affectionately called MUC.
21 * It contains the components that are required 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 store interface.
34 * This interface defines the static methods that must be implemented by every cache store plugin.
35 * To ensure plugins implement this class the abstract cache_store class implements this interface.
37 * @package core
38 * @category cache
39 * @copyright 2012 Sam Hemelryk
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 interface cache_store_interface {
43 /**
44 * Static method to check if the store requirements are met.
46 * @return bool True if the stores software/hardware requirements have been met and it can be used. False otherwise.
48 public static function are_requirements_met();
50 /**
51 * Static method to check if a store is usable with the given mode.
53 * @param int $mode One of cache_store::MODE_*
55 public static function is_supported_mode($mode);
57 /**
58 * Returns the supported features as a binary flag.
60 * @param array $configuration The configuration of a store to consider specifically.
61 * @return int The supported features.
63 public static function get_supported_features(array $configuration = array());
65 /**
66 * Returns the supported modes as a binary flag.
68 * @param array $configuration The configuration of a store to consider specifically.
69 * @return int The supported modes.
71 public static function get_supported_modes(array $configuration = array());
73 /**
74 * Generates an instance of the cache store that can be used for testing.
76 * Returns an instance of the cache store, or false if one cannot be created.
78 * @param cache_definition $definition
79 * @return cache_store|false
81 public static function initialise_test_instance(cache_definition $definition);
83 /**
84 * Generates the appropriate configuration required for unit testing.
86 * @return array Array of unit test configuration data to be used by initialise().
88 public static function unit_test_configuration();
91 /**
92 * Abstract cache store class.
94 * All cache store plugins must extend this base class.
95 * It lays down the foundation for what is required of a cache store plugin.
97 * @since Moodle 2.4
98 * @package core
99 * @category cache
100 * @copyright 2012 Sam Hemelryk
101 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
103 abstract class cache_store implements cache_store_interface {
105 // Constants for features a cache store can support
108 * Supports multi-part keys
110 const SUPPORTS_MULTIPLE_IDENTIFIERS = 1;
112 * Ensures data remains in the cache once set.
114 const SUPPORTS_DATA_GUARANTEE = 2;
116 * Supports a native ttl system.
118 const SUPPORTS_NATIVE_TTL = 4;
121 * The cache is searchable by key.
123 const IS_SEARCHABLE = 8;
126 * The cache store dereferences objects.
128 * When set, loaders will assume that all data coming from this store has already had all references
129 * resolved. So even for complex object structures it will not try to remove references again.
131 const DEREFERENCES_OBJECTS = 16;
133 // Constants for the modes of a cache store
136 * Application caches. These are shared caches.
138 const MODE_APPLICATION = 1;
140 * Session caches. Just access to the PHP session.
142 const MODE_SESSION = 2;
144 * Request caches. Static caches really.
146 const MODE_REQUEST = 4;
149 * Constructs an instance of the cache store.
151 * The constructor should be responsible for creating anything needed by the store that is not
152 * specific to a definition.
153 * Tasks such as opening a connection to check it is available are best done here.
154 * Tasks that are definition specific such as creating a storage area for the definition data
155 * or creating key tables and indexs are best done within the initialise method.
157 * Once a store has been constructed the cache API will check it is ready to be intialised with
158 * a definition by called $this->is_ready().
159 * If the setup of the store failed (connection could not be established for example) then
160 * that method should return false so that the store instance is not selected for use.
162 * @param string $name The name of the cache store
163 * @param array $configuration The configuration for this store instance.
165 abstract public function __construct($name, array $configuration = array());
168 * Returns the name of this store instance.
169 * @return string
171 abstract public function my_name();
174 * Initialises a new instance of the cache store given the definition the instance is to be used for.
176 * This function should be used to run any definition specific setup the store instance requires.
177 * Tasks such as creating storage areas, or creating indexes are best done here.
179 * Its important to note that the initialise method is expected to always succeed.
180 * If there are setup tasks that may fail they should be done within the __construct method
181 * and should they fail is_ready should return false.
183 * @param cache_definition $definition
185 abstract public function initialise(cache_definition $definition);
188 * Returns true if this cache store instance has been initialised.
189 * @return bool
191 abstract public function is_initialised();
194 * Returns true if this cache store instance is ready to use.
195 * @return bool
197 public function is_ready() {
198 return forward_static_call(array($this, 'are_requirements_met'));
202 * Retrieves an item from the cache store given its key.
204 * @param string $key The key to retrieve
205 * @return mixed The data that was associated with the key, or false if the key did not exist.
207 abstract public function get($key);
210 * Retrieves several items from the cache store in a single transaction.
212 * If not all of the items are available in the cache then the data value for those that are missing will be set to false.
214 * @param array $keys The array of keys to retrieve
215 * @return array An array of items from the cache. There will be an item for each key, those that were not in the store will
216 * be set to false.
218 abstract public function get_many($keys);
221 * Sets an item in the cache given its key and data value.
223 * @param string $key The key to use.
224 * @param mixed $data The data to set.
225 * @return bool True if the operation was a success false otherwise.
227 abstract public function set($key, $data);
230 * Sets many items in the cache in a single transaction.
232 * @param array $keyvaluearray An array of key value pairs. Each item in the array will be an associative array with two
233 * keys, 'key' and 'value'.
234 * @return int The number of items successfully set. It is up to the developer to check this matches the number of items
235 * sent ... if they care that is.
237 abstract public function set_many(array $keyvaluearray);
240 * Deletes an item from the cache store.
242 * @param string $key The key to delete.
243 * @return bool Returns true if the operation was a success, false otherwise.
245 abstract public function delete($key);
248 * Deletes several keys from the cache in a single action.
250 * @param array $keys The keys to delete
251 * @return int The number of items successfully deleted.
253 abstract public function delete_many(array $keys);
256 * Purges the cache deleting all items within it.
258 * @return boolean True on success. False otherwise.
260 abstract public function purge();
263 * @deprecated since 2.5
264 * @see \cache_store::instance_deleted()
266 public function cleanup() {
267 throw new coding_exception('cache_store::cleanup() can not be used anymore.' .
268 ' Please use cache_store::instance_deleted() instead.');
272 * Performs any necessary operation when the store instance has been created.
274 * @since Moodle 2.5
276 public function instance_created() {
277 // By default, do nothing.
281 * Performs any necessary operation when the store instance is being deleted.
283 * This method may be called before the store has been initialised.
285 * @since Moodle 2.5
286 * @see cleanup()
288 public function instance_deleted() {
289 if (method_exists($this, 'cleanup')) {
290 // There used to be a legacy function called cleanup, it was renamed to instance delete.
291 // To be removed in 2.6.
292 $this->cleanup();
297 * Returns true if the user can add an instance of the store plugin.
299 * @return bool
301 public static function can_add_instance() {
302 return true;
306 * Returns true if the store instance guarantees data.
308 * @return bool
310 public function supports_data_guarantee() {
311 return $this::get_supported_features() & self::SUPPORTS_DATA_GUARANTEE;
315 * Returns true if the store instance supports multiple identifiers.
317 * @return bool
319 public function supports_multiple_identifiers() {
320 return $this::get_supported_features() & self::SUPPORTS_MULTIPLE_IDENTIFIERS;
324 * Returns true if the store instance supports native ttl.
326 * @return bool
328 public function supports_native_ttl() {
329 return $this::get_supported_features() & self::SUPPORTS_NATIVE_TTL;
333 * Returns true if the store instance is searchable.
335 * @return bool
337 public function is_searchable() {
338 return in_array('cache_is_searchable', class_implements($this));
342 * Returns true if the store automatically dereferences objects.
344 * @return bool
346 public function supports_dereferencing_objects() {
347 return $this::get_supported_features() & self::DEREFERENCES_OBJECTS;
351 * Creates a clone of this store instance ready to be initialised.
353 * This method is used so that a cache store needs only be constructed once.
354 * Future requests for an instance of the store will be given a cloned instance.
356 * If you are writing a cache store that isn't compatible with the clone operation
357 * you can override this method to handle any situations you want before cloning.
359 * @param array $details An array containing the details of the store from the cache config.
360 * @return cache_store
362 public function create_clone(array $details = array()) {
363 // By default we just run clone.
364 // Any stores that have an issue with this will need to override the create_clone method.
365 return clone($this);
369 * Can be overridden to return any warnings this store instance should make to the admin.
371 * This should be used to notify things like configuration conflicts etc.
372 * The warnings returned here will be displayed on the cache configuration screen.
374 * @return string[] An array of warning strings from the store instance.
376 public function get_warnings() {
377 return array();
381 * Returns true if this cache store instance is both suitable for testing, and ready for testing.
383 * Cache stores that support being used as the default store for unit and acceptance testing should
384 * override this function and return true if there requirements have been met.
386 * @return bool
388 public static function ready_to_be_used_for_testing() {
389 return false;