Merge branch 'MDL-40555-24' of git://github.com/FMCorz/moodle into MOODLE_24_STABLE
[moodle.git] / cache / disabledlib.php
blob18cc11eb07da93f3b07ad966736eb969d4c7e4f7
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 * This file contains classes that are used by the Cache API only when it is disabled.
20 * These classes are derivatives of other significant classes used by the Cache API customised specifically
21 * to only do what is absolutely necessary when initialising and using the Cache API when its been disabled.
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 * Required as it is needed for cache_config_disabled which extends cache_config_writer.
34 require_once($CFG->dirroot.'/cache/locallib.php');
36 /**
37 * The cache loader class used when the Cache has been disabled.
39 * @copyright 2012 Sam Hemelryk
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class cache_disabled extends cache {
44 /**
45 * Constructs the cache.
47 * @param cache_definition $definition
48 * @param cache_store $store
49 * @param null $loader Unused.
51 public function __construct(cache_definition $definition, cache_store $store, $loader = null) {
52 $this->definition = $definition;
53 $this->store = $store;
56 /**
57 * Gets a key from the cache.
59 * @param int|string $key
60 * @param int $strictness Unused.
61 * @return bool
63 public function get($key, $strictness = IGNORE_MISSING) {
64 return false;
67 /**
68 * Gets many keys at once from the cache.
70 * @param array $keys
71 * @param int $strictness Unused.
72 * @return array
74 public function get_many(array $keys, $strictness = IGNORE_MISSING) {
75 $return = array();
76 foreach ($keys as $key) {
77 $return[$key] = false;
79 return $return;
82 /**
83 * Sets a key value pair in the cache.
85 * @param int|string $key Unused.
86 * @param mixed $data Unused.
87 * @return bool
89 public function set($key, $data) {
90 return false;
93 /**
94 * Sets many key value pairs in the cache at once.
96 * @param array $keyvaluearray Unused.
97 * @return int
99 public function set_many(array $keyvaluearray) {
100 return 0;
104 * Deletes an item from the cache.
106 * @param int|string $key Unused.
107 * @param bool $recurse Unused.
108 * @return bool
110 public function delete($key, $recurse = true) {
111 return false;
115 * Deletes many items at once from the cache.
117 * @param array $keys Unused.
118 * @param bool $recurse Unused.
119 * @return int
121 public function delete_many(array $keys, $recurse = true) {
122 return 0;
126 * Checks if the cache has the requested key.
128 * @param int|string $key Unused.
129 * @return bool
131 public function has($key) {
132 return false;
136 * Checks if the cache has all of the requested keys.
137 * @param array $keys Unused.
138 * @return bool
140 public function has_all(array $keys) {
141 return false;
145 * Checks if the cache has any of the requested keys.
147 * @param array $keys Unused.
148 * @return bool
150 public function has_any(array $keys) {
151 return false;
155 * Purges all items from the cache.
157 * @return bool
159 public function purge() {
160 return true;
165 * The cache factory class used when the Cache has been disabled.
167 * @copyright 2012 Sam Hemelryk
168 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
170 class cache_factory_disabled extends cache_factory {
173 * Returns an instance of the cache_factor method.
175 * @param bool $forcereload Unused.
176 * @return cache_factory
177 * @throws coding_exception
179 public static function instance($forcereload = false) {
180 throw new coding_exception('You must not call to this cache factory within your code.');
184 * Creates a definition instance or returns the existing one if it has already been created.
186 * @param string $component
187 * @param string $area
188 * @param string $aggregate Unused.
189 * @return cache_definition
191 public function create_definition($component, $area, $aggregate = null) {
192 return cache_definition::load_adhoc(cache_store::MODE_REQUEST, $component, $area);
196 * Common public method to create a cache instance given a definition.
198 * @param cache_definition $definition
199 * @return cache_application|cache_session|cache_store
200 * @throws coding_exception
202 public function create_cache(cache_definition $definition) {
203 return new cache_disabled($definition, $this->create_dummy_store($definition));
207 * Creates a cache object given the parameters for a definition.
209 * @param string $component
210 * @param string $area
211 * @param array $identifiers
212 * @param string $aggregate
213 * @return cache_application|cache_session|cache_request
215 public function create_cache_from_definition($component, $area, array $identifiers = array(), $aggregate = null) {
216 $definition = $this->create_definition($component, $area, $aggregate);
217 $cache = $this->create_cache($definition, $identifiers);
218 return $cache;
222 * Creates an ad-hoc cache from the given param.
224 * @param int $mode
225 * @param string $component
226 * @param string $area
227 * @param array $identifiers
228 * @param array $options An array of options, available options are:
229 * - simplekeys : Set to true if the keys you will use are a-zA-Z0-9_
230 * - simpledata : Set to true if the type of the data you are going to store is scalar, or an array of scalar vars
231 * - persistent : If set to true the cache will persist construction requests.
232 * @return cache_application|cache_session|cache_request
234 public function create_cache_from_params($mode, $component, $area, array $identifiers = array(), array $options = array()) {
235 $definition = cache_definition::load_adhoc($mode, $component, $area);
236 $cache = $this->create_cache($definition, $identifiers);
237 return $cache;
241 * Creates a store instance given its name and configuration.
243 * @param string $name Unused.
244 * @param array $details Unused.
245 * @param cache_definition $definition
246 * @return boolean|cache_store
248 public function create_store_from_config($name, array $details, cache_definition $definition) {
249 return $this->create_dummy_store($definition);
253 * Creates a cache config instance with the ability to write if required.
255 * @param bool $writer Unused.
256 * @return cache_config|cache_config_writer
258 public function create_config_instance($writer = false) {
259 $class = 'cache_config_disabled';
260 if (!array_key_exists($class, $this->configs)) {
261 self::set_state(self::STATE_INITIALISING);
262 $configuration = $class::create_default_configuration();
263 $this->configs[$class] = new $class;
264 $this->configs[$class]->load($configuration);
266 self::set_state(self::STATE_READY);
268 // Return the instance.
269 return $this->configs[$class];
274 * The cache config class used when the Cache has been disabled.
276 * @copyright 2012 Sam Hemelryk
277 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
279 class cache_config_disabled extends cache_config_writer {
282 * Returns an instance of the configuration writer.
284 * @return cache_config_disabled
286 public static function instance() {
287 $factory = cache_factory::instance();
288 return $factory->create_config_instance(true);
292 * Saves the current configuration.
294 protected function config_save() {
295 // Nothing to do here.
299 * Generates a configuration array suitable to be written to the config file.
301 * @return array
303 protected function generate_configuration_array() {
304 $configuration = array();
305 $configuration['stores'] = $this->configstores;
306 $configuration['modemappings'] = $this->configmodemappings;
307 $configuration['definitions'] = $this->configdefinitions;
308 $configuration['definitionmappings'] = $this->configdefinitionmappings;
309 $configuration['locks'] = $this->configlocks;
310 return $configuration;
314 * Adds a plugin instance.
316 * @param string $name Unused.
317 * @param string $plugin Unused.
318 * @param array $configuration Unused.
319 * @return bool
320 * @throws cache_exception
322 public function add_store_instance($name, $plugin, array $configuration = array()) {
323 return false;
327 * Sets the mode mappings.
329 * @param array $modemappings Unused.
330 * @return bool
331 * @throws cache_exception
333 public function set_mode_mappings(array $modemappings) {
334 return false;
338 * Edits a give plugin instance.
340 * @param string $name Unused.
341 * @param string $plugin Unused.
342 * @param array $configuration Unused.
343 * @return bool
344 * @throws cache_exception
346 public function edit_store_instance($name, $plugin, $configuration) {
347 return false;
351 * Deletes a store instance.
353 * @param string $name Unused.
354 * @return bool
355 * @throws cache_exception
357 public function delete_store_instance($name) {
358 return false;
362 * Creates the default configuration and saves it.
364 * @return array
366 public static function create_default_configuration() {
367 global $CFG;
369 // HACK ALERT.
370 // We probably need to come up with a better way to create the default stores, or at least ensure 100% that the
371 // default store plugins are protected from deletion.
372 require_once($CFG->dirroot.'/cache/stores/file/lib.php');
373 require_once($CFG->dirroot.'/cache/stores/session/lib.php');
374 require_once($CFG->dirroot.'/cache/stores/static/lib.php');
376 $writer = new self;
377 $writer->configstores = array(
378 'default_application' => array(
379 'name' => 'default_application',
380 'plugin' => 'file',
381 'configuration' => array(),
382 'features' => cachestore_file::get_supported_features(),
383 'modes' => cache_store::MODE_APPLICATION,
384 'default' => true,
386 'default_session' => array(
387 'name' => 'default_session',
388 'plugin' => 'session',
389 'configuration' => array(),
390 'features' => cachestore_session::get_supported_features(),
391 'modes' => cache_store::MODE_SESSION,
392 'default' => true,
394 'default_request' => array(
395 'name' => 'default_request',
396 'plugin' => 'static',
397 'configuration' => array(),
398 'features' => cachestore_static::get_supported_features(),
399 'modes' => cache_store::MODE_REQUEST,
400 'default' => true,
403 $writer->configdefinitions = array();
404 $writer->configmodemappings = array(
405 array(
406 'mode' => cache_store::MODE_APPLICATION,
407 'store' => 'default_application',
408 'sort' => -1
410 array(
411 'mode' => cache_store::MODE_SESSION,
412 'store' => 'default_session',
413 'sort' => -1
415 array(
416 'mode' => cache_store::MODE_REQUEST,
417 'store' => 'default_request',
418 'sort' => -1
421 $writer->configlocks = array(
422 'default_file_lock' => array(
423 'name' => 'cachelock_file_default',
424 'type' => 'cachelock_file',
425 'dir' => 'filelocks',
426 'default' => true
430 return $writer->generate_configuration_array();
434 * Updates the definition in the configuration from those found in the cache files.
436 * @param bool $coreonly Unused.
438 public static function update_definitions($coreonly = false) {
439 // Nothing to do here.
443 * Locates all of the definition files.
445 * @param bool $coreonly Unused.
446 * @return array
448 protected static function locate_definitions($coreonly = false) {
449 return array();
453 * Sets the mappings for a given definition.
455 * @param string $definition Unused.
456 * @param array $mappings Unused.
457 * @throws coding_exception
459 public function set_definition_mappings($definition, $mappings) {
460 // Nothing to do here.