weekly release 4.0dev
[moodle.git] / cache / disabledlib.php
CommitLineData
3680c61a
SH
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 * This file contains classes that are used by the Cache API only when it is disabled.
19 *
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.
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
29defined('MOODLE_INTERNAL') || die();
30
31/**
32 * Required as it is needed for cache_config_disabled which extends cache_config_writer.
33 */
34require_once($CFG->dirroot.'/cache/locallib.php');
35
36/**
37 * The cache loader class used when the Cache has been disabled.
38 *
39 * @copyright 2012 Sam Hemelryk
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class cache_disabled extends cache {
43
44 /**
45 * Constructs the cache.
46 *
47 * @param cache_definition $definition
48 * @param cache_store $store
49 * @param null $loader Unused.
50 */
51 public function __construct(cache_definition $definition, cache_store $store, $loader = null) {
d074fb09 52 // Nothing to do here.
3680c61a
SH
53 }
54
55 /**
56 * Gets a key from the cache.
57 *
58 * @param int|string $key
59 * @param int $strictness Unused.
60 * @return bool
61 */
62 public function get($key, $strictness = IGNORE_MISSING) {
63 return false;
64 }
65
66 /**
67 * Gets many keys at once from the cache.
68 *
69 * @param array $keys
70 * @param int $strictness Unused.
71 * @return array
72 */
73 public function get_many(array $keys, $strictness = IGNORE_MISSING) {
74 $return = array();
75 foreach ($keys as $key) {
76 $return[$key] = false;
77 }
78 return $return;
79 }
80
81 /**
82 * Sets a key value pair in the cache.
83 *
84 * @param int|string $key Unused.
85 * @param mixed $data Unused.
86 * @return bool
87 */
88 public function set($key, $data) {
89 return false;
90 }
91
92 /**
93 * Sets many key value pairs in the cache at once.
94 *
95 * @param array $keyvaluearray Unused.
96 * @return int
97 */
98 public function set_many(array $keyvaluearray) {
99 return 0;
100 }
101
102 /**
103 * Deletes an item from the cache.
104 *
105 * @param int|string $key Unused.
106 * @param bool $recurse Unused.
107 * @return bool
108 */
109 public function delete($key, $recurse = true) {
110 return false;
111 }
112
113 /**
114 * Deletes many items at once from the cache.
115 *
116 * @param array $keys Unused.
117 * @param bool $recurse Unused.
118 * @return int
119 */
120 public function delete_many(array $keys, $recurse = true) {
121 return 0;
122 }
123
124 /**
125 * Checks if the cache has the requested key.
126 *
127 * @param int|string $key Unused.
78da366b 128 * @param bool $tryloadifpossible Unused.
3680c61a
SH
129 * @return bool
130 */
78da366b 131 public function has($key, $tryloadifpossible = false) {
3680c61a
SH
132 return false;
133 }
134
135 /**
136 * Checks if the cache has all of the requested keys.
137 * @param array $keys Unused.
138 * @return bool
139 */
140 public function has_all(array $keys) {
141 return false;
142 }
143
144 /**
145 * Checks if the cache has any of the requested keys.
146 *
147 * @param array $keys Unused.
148 * @return bool
149 */
150 public function has_any(array $keys) {
151 return false;
152 }
153
154 /**
155 * Purges all items from the cache.
156 *
157 * @return bool
158 */
159 public function purge() {
160 return true;
161 }
162}
163
164/**
165 * The cache factory class used when the Cache has been disabled.
166 *
167 * @copyright 2012 Sam Hemelryk
168 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
169 */
170class cache_factory_disabled extends cache_factory {
171
172 /**
173 * Returns an instance of the cache_factor method.
174 *
175 * @param bool $forcereload Unused.
176 * @return cache_factory
177 * @throws coding_exception
178 */
179 public static function instance($forcereload = false) {
180 throw new coding_exception('You must not call to this cache factory within your code.');
181 }
182
183 /**
184 * Creates a definition instance or returns the existing one if it has already been created.
185 *
186 * @param string $component
187 * @param string $area
83bee881 188 * @param string $unused Used to be datasourceaggregate but that was removed and this is now unused.
3680c61a
SH
189 * @return cache_definition
190 */
83bee881 191 public function create_definition($component, $area, $unused = null) {
3680c61a
SH
192 return cache_definition::load_adhoc(cache_store::MODE_REQUEST, $component, $area);
193 }
194
195 /**
196 * Common public method to create a cache instance given a definition.
197 *
198 * @param cache_definition $definition
199 * @return cache_application|cache_session|cache_store
200 * @throws coding_exception
201 */
202 public function create_cache(cache_definition $definition) {
203 return new cache_disabled($definition, $this->create_dummy_store($definition));
204 }
205
206 /**
207 * Creates a cache object given the parameters for a definition.
208 *
209 * @param string $component
210 * @param string $area
211 * @param array $identifiers
83bee881 212 * @param string $unused Used to be datasourceaggregate but that was removed and this is now unused.
3680c61a
SH
213 * @return cache_application|cache_session|cache_request
214 */
83bee881 215 public function create_cache_from_definition($component, $area, array $identifiers = array(), $unused = null) {
f3789f2f
RS
216 // Regular cache definitions are cached inside create_definition(). This is not the case for disabledlib.php
217 // definitions as they use load_adhoc(). They are built as a new object on each call.
218 // We do not need to clone the definition because we know it's new.
83bee881 219 $definition = $this->create_definition($component, $area);
ca9a79da
RT
220 $definition->set_identifiers($identifiers);
221 $cache = $this->create_cache($definition);
3680c61a
SH
222 return $cache;
223 }
224
225 /**
226 * Creates an ad-hoc cache from the given param.
227 *
228 * @param int $mode
229 * @param string $component
230 * @param string $area
231 * @param array $identifiers
94ef67cf
SH
232 * @param array $options An array of options, available options are:
233 * - simplekeys : Set to true if the keys you will use are a-zA-Z0-9_
234 * - simpledata : Set to true if the type of the data you are going to store is scalar, or an array of scalar vars
d074fb09
SH
235 * - staticacceleration : If set to true the cache will hold onto all data passing through it.
236 * - staticaccelerationsize : Sets the max size of the static acceleration array.
3680c61a
SH
237 * @return cache_application|cache_session|cache_request
238 */
94ef67cf 239 public function create_cache_from_params($mode, $component, $area, array $identifiers = array(), array $options = array()) {
f3789f2f
RS
240 // Regular cache definitions are cached inside create_definition(). This is not the case for disabledlib.php
241 // definitions as they use load_adhoc(). They are built as a new object on each call.
242 // We do not need to clone the definition because we know it's new.
243 $definition = cache_definition::load_adhoc($mode, $component, $area, $options);
ca9a79da
RT
244 $definition->set_identifiers($identifiers);
245 $cache = $this->create_cache($definition);
3680c61a
SH
246 return $cache;
247 }
248
249 /**
250 * Creates a store instance given its name and configuration.
251 *
252 * @param string $name Unused.
253 * @param array $details Unused.
254 * @param cache_definition $definition
255 * @return boolean|cache_store
256 */
257 public function create_store_from_config($name, array $details, cache_definition $definition) {
258 return $this->create_dummy_store($definition);
259 }
260
261 /**
262 * Creates a cache config instance with the ability to write if required.
263 *
264 * @param bool $writer Unused.
b0dd08dd 265 * @return cache_config_disabled|cache_config_writer
3680c61a
SH
266 */
267 public function create_config_instance($writer = false) {
c6ec9309
SH
268 // We are always going to use the cache_config_disabled class for all regular request.
269 // However if the code has requested the writer then likely something is changing and
270 // we're going to need to interact with the config.php file.
271 // In this case we will still use the cache_config_writer.
3680c61a 272 $class = 'cache_config_disabled';
c6ec9309
SH
273 if ($writer) {
274 // If the writer was requested then something is changing.
275 $class = 'cache_config_writer';
276 }
3680c61a
SH
277 if (!array_key_exists($class, $this->configs)) {
278 self::set_state(self::STATE_INITIALISING);
b0dd08dd 279 if ($class === 'cache_config_disabled') {
c6ec9309
SH
280 $configuration = $class::create_default_configuration();
281 } else {
282 $configuration = false;
b0dd08dd
SH
283 if (!cache_config::config_file_exists()) {
284 cache_config_writer::create_default_configuration(true);
285 }
c6ec9309 286 }
3680c61a
SH
287 $this->configs[$class] = new $class;
288 $this->configs[$class]->load($configuration);
289 }
290 self::set_state(self::STATE_READY);
291
292 // Return the instance.
293 return $this->configs[$class];
294 }
295}
296
297/**
298 * The cache config class used when the Cache has been disabled.
299 *
300 * @copyright 2012 Sam Hemelryk
301 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
302 */
303class cache_config_disabled extends cache_config_writer {
304
305 /**
306 * Returns an instance of the configuration writer.
307 *
308 * @return cache_config_disabled
309 */
310 public static function instance() {
311 $factory = cache_factory::instance();
312 return $factory->create_config_instance(true);
313 }
314
315 /**
316 * Saves the current configuration.
317 */
318 protected function config_save() {
319 // Nothing to do here.
320 }
321
322 /**
323 * Generates a configuration array suitable to be written to the config file.
324 *
325 * @return array
326 */
327 protected function generate_configuration_array() {
328 $configuration = array();
329 $configuration['stores'] = $this->configstores;
330 $configuration['modemappings'] = $this->configmodemappings;
331 $configuration['definitions'] = $this->configdefinitions;
332 $configuration['definitionmappings'] = $this->configdefinitionmappings;
333 $configuration['locks'] = $this->configlocks;
334 return $configuration;
335 }
336
337 /**
338 * Adds a plugin instance.
339 *
340 * @param string $name Unused.
341 * @param string $plugin Unused.
342 * @param array $configuration Unused.
343 * @return bool
344 * @throws cache_exception
345 */
346 public function add_store_instance($name, $plugin, array $configuration = array()) {
347 return false;
348 }
349
350 /**
351 * Sets the mode mappings.
352 *
353 * @param array $modemappings Unused.
354 * @return bool
355 * @throws cache_exception
356 */
357 public function set_mode_mappings(array $modemappings) {
358 return false;
359 }
360
361 /**
362 * Edits a give plugin instance.
363 *
364 * @param string $name Unused.
365 * @param string $plugin Unused.
366 * @param array $configuration Unused.
367 * @return bool
368 * @throws cache_exception
369 */
370 public function edit_store_instance($name, $plugin, $configuration) {
371 return false;
372 }
373
374 /**
375 * Deletes a store instance.
376 *
377 * @param string $name Unused.
378 * @return bool
379 * @throws cache_exception
380 */
381 public function delete_store_instance($name) {
382 return false;
383 }
384
385 /**
386 * Creates the default configuration and saves it.
387 *
b0dd08dd 388 * @param bool $forcesave Ignored because we are disabled!
3680c61a
SH
389 * @return array
390 */
b0dd08dd 391 public static function create_default_configuration($forcesave = false) {
3680c61a
SH
392 global $CFG;
393
394 // HACK ALERT.
395 // We probably need to come up with a better way to create the default stores, or at least ensure 100% that the
396 // default store plugins are protected from deletion.
397 require_once($CFG->dirroot.'/cache/stores/file/lib.php');
398 require_once($CFG->dirroot.'/cache/stores/session/lib.php');
399 require_once($CFG->dirroot.'/cache/stores/static/lib.php');
400
401 $writer = new self;
402 $writer->configstores = array(
403 'default_application' => array(
404 'name' => 'default_application',
405 'plugin' => 'file',
406 'configuration' => array(),
407 'features' => cachestore_file::get_supported_features(),
408 'modes' => cache_store::MODE_APPLICATION,
409 'default' => true,
410 ),
411 'default_session' => array(
412 'name' => 'default_session',
413 'plugin' => 'session',
414 'configuration' => array(),
415 'features' => cachestore_session::get_supported_features(),
416 'modes' => cache_store::MODE_SESSION,
417 'default' => true,
418 ),
419 'default_request' => array(
420 'name' => 'default_request',
421 'plugin' => 'static',
422 'configuration' => array(),
423 'features' => cachestore_static::get_supported_features(),
424 'modes' => cache_store::MODE_REQUEST,
425 'default' => true,
426 )
427 );
428 $writer->configdefinitions = array();
429 $writer->configmodemappings = array(
430 array(
431 'mode' => cache_store::MODE_APPLICATION,
432 'store' => 'default_application',
433 'sort' => -1
434 ),
435 array(
436 'mode' => cache_store::MODE_SESSION,
437 'store' => 'default_session',
438 'sort' => -1
439 ),
440 array(
441 'mode' => cache_store::MODE_REQUEST,
442 'store' => 'default_request',
443 'sort' => -1
444 )
445 );
446 $writer->configlocks = array(
447 'default_file_lock' => array(
448 'name' => 'cachelock_file_default',
449 'type' => 'cachelock_file',
450 'dir' => 'filelocks',
451 'default' => true
452 )
453 );
454
455 return $writer->generate_configuration_array();
456 }
457
458 /**
459 * Updates the definition in the configuration from those found in the cache files.
460 *
461 * @param bool $coreonly Unused.
462 */
463 public static function update_definitions($coreonly = false) {
464 // Nothing to do here.
465 }
466
467 /**
468 * Locates all of the definition files.
469 *
470 * @param bool $coreonly Unused.
471 * @return array
472 */
473 protected static function locate_definitions($coreonly = false) {
474 return array();
475 }
476
477 /**
478 * Sets the mappings for a given definition.
479 *
480 * @param string $definition Unused.
481 * @param array $mappings Unused.
482 * @throws coding_exception
483 */
484 public function set_definition_mappings($definition, $mappings) {
485 // Nothing to do here.
486 }
487}