MDL-36466 cache: implemented functionality to disable the bulk of the cache API
[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) {
52 $this->definition = $definition;
53 $this->store = $store;
54 }
55
56 /**
57 * Gets a key from the cache.
58 *
59 * @param int|string $key
60 * @param int $strictness Unused.
61 * @return bool
62 */
63 public function get($key, $strictness = IGNORE_MISSING) {
64 return false;
65 }
66
67 /**
68 * Gets many keys at once from the cache.
69 *
70 * @param array $keys
71 * @param int $strictness Unused.
72 * @return array
73 */
74 public function get_many(array $keys, $strictness = IGNORE_MISSING) {
75 $return = array();
76 foreach ($keys as $key) {
77 $return[$key] = false;
78 }
79 return $return;
80 }
81
82 /**
83 * Sets a key value pair in the cache.
84 *
85 * @param int|string $key Unused.
86 * @param mixed $data Unused.
87 * @return bool
88 */
89 public function set($key, $data) {
90 return false;
91 }
92
93 /**
94 * Sets many key value pairs in the cache at once.
95 *
96 * @param array $keyvaluearray Unused.
97 * @return int
98 */
99 public function set_many(array $keyvaluearray) {
100 return 0;
101 }
102
103 /**
104 * Deletes an item from the cache.
105 *
106 * @param int|string $key Unused.
107 * @param bool $recurse Unused.
108 * @return bool
109 */
110 public function delete($key, $recurse = true) {
111 return false;
112 }
113
114 /**
115 * Deletes many items at once from the cache.
116 *
117 * @param array $keys Unused.
118 * @param bool $recurse Unused.
119 * @return int
120 */
121 public function delete_many(array $keys, $recurse = true) {
122 return 0;
123 }
124
125 /**
126 * Checks if the cache has the requested key.
127 *
128 * @param int|string $key Unused.
129 * @return bool
130 */
131 public function has($key) {
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
188 * @param string $aggregate Unused.
189 * @return cache_definition
190 */
191 public function create_definition($component, $area, $aggregate = null) {
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
212 * @param string $aggregate
213 * @return cache_application|cache_session|cache_request
214 */
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;
219 }
220
221 /**
222 * Creates an ad-hoc cache from the given param.
223 *
224 * @param int $mode
225 * @param string $component
226 * @param string $area
227 * @param array $identifiers
228 * @param bool $persistent Unused.
229 * @return cache_application|cache_session|cache_request
230 */
231 public function create_cache_from_params($mode, $component, $area, array $identifiers = array(), $persistent = false) {
232 $definition = cache_definition::load_adhoc($mode, $component, $area);
233 $cache = $this->create_cache($definition, $identifiers);
234 return $cache;
235 }
236
237 /**
238 * Creates a store instance given its name and configuration.
239 *
240 * @param string $name Unused.
241 * @param array $details Unused.
242 * @param cache_definition $definition
243 * @return boolean|cache_store
244 */
245 public function create_store_from_config($name, array $details, cache_definition $definition) {
246 return $this->create_dummy_store($definition);
247 }
248
249 /**
250 * Creates a cache config instance with the ability to write if required.
251 *
252 * @param bool $writer Unused.
253 * @return cache_config|cache_config_writer
254 */
255 public function create_config_instance($writer = false) {
256 $class = 'cache_config_disabled';
257 if (!array_key_exists($class, $this->configs)) {
258 self::set_state(self::STATE_INITIALISING);
259 $configuration = $class::create_default_configuration();
260 $this->configs[$class] = new $class;
261 $this->configs[$class]->load($configuration);
262 }
263 self::set_state(self::STATE_READY);
264
265 // Return the instance.
266 return $this->configs[$class];
267 }
268}
269
270/**
271 * The cache config class used when the Cache has been disabled.
272 *
273 * @copyright 2012 Sam Hemelryk
274 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
275 */
276class cache_config_disabled extends cache_config_writer {
277
278 /**
279 * Returns an instance of the configuration writer.
280 *
281 * @return cache_config_disabled
282 */
283 public static function instance() {
284 $factory = cache_factory::instance();
285 return $factory->create_config_instance(true);
286 }
287
288 /**
289 * Saves the current configuration.
290 */
291 protected function config_save() {
292 // Nothing to do here.
293 }
294
295 /**
296 * Generates a configuration array suitable to be written to the config file.
297 *
298 * @return array
299 */
300 protected function generate_configuration_array() {
301 $configuration = array();
302 $configuration['stores'] = $this->configstores;
303 $configuration['modemappings'] = $this->configmodemappings;
304 $configuration['definitions'] = $this->configdefinitions;
305 $configuration['definitionmappings'] = $this->configdefinitionmappings;
306 $configuration['locks'] = $this->configlocks;
307 return $configuration;
308 }
309
310 /**
311 * Adds a plugin instance.
312 *
313 * @param string $name Unused.
314 * @param string $plugin Unused.
315 * @param array $configuration Unused.
316 * @return bool
317 * @throws cache_exception
318 */
319 public function add_store_instance($name, $plugin, array $configuration = array()) {
320 return false;
321 }
322
323 /**
324 * Sets the mode mappings.
325 *
326 * @param array $modemappings Unused.
327 * @return bool
328 * @throws cache_exception
329 */
330 public function set_mode_mappings(array $modemappings) {
331 return false;
332 }
333
334 /**
335 * Edits a give plugin instance.
336 *
337 * @param string $name Unused.
338 * @param string $plugin Unused.
339 * @param array $configuration Unused.
340 * @return bool
341 * @throws cache_exception
342 */
343 public function edit_store_instance($name, $plugin, $configuration) {
344 return false;
345 }
346
347 /**
348 * Deletes a store instance.
349 *
350 * @param string $name Unused.
351 * @return bool
352 * @throws cache_exception
353 */
354 public function delete_store_instance($name) {
355 return false;
356 }
357
358 /**
359 * Creates the default configuration and saves it.
360 *
361 * @return array
362 */
363 public static function create_default_configuration() {
364 global $CFG;
365
366 // HACK ALERT.
367 // We probably need to come up with a better way to create the default stores, or at least ensure 100% that the
368 // default store plugins are protected from deletion.
369 require_once($CFG->dirroot.'/cache/stores/file/lib.php');
370 require_once($CFG->dirroot.'/cache/stores/session/lib.php');
371 require_once($CFG->dirroot.'/cache/stores/static/lib.php');
372
373 $writer = new self;
374 $writer->configstores = array(
375 'default_application' => array(
376 'name' => 'default_application',
377 'plugin' => 'file',
378 'configuration' => array(),
379 'features' => cachestore_file::get_supported_features(),
380 'modes' => cache_store::MODE_APPLICATION,
381 'default' => true,
382 ),
383 'default_session' => array(
384 'name' => 'default_session',
385 'plugin' => 'session',
386 'configuration' => array(),
387 'features' => cachestore_session::get_supported_features(),
388 'modes' => cache_store::MODE_SESSION,
389 'default' => true,
390 ),
391 'default_request' => array(
392 'name' => 'default_request',
393 'plugin' => 'static',
394 'configuration' => array(),
395 'features' => cachestore_static::get_supported_features(),
396 'modes' => cache_store::MODE_REQUEST,
397 'default' => true,
398 )
399 );
400 $writer->configdefinitions = array();
401 $writer->configmodemappings = array(
402 array(
403 'mode' => cache_store::MODE_APPLICATION,
404 'store' => 'default_application',
405 'sort' => -1
406 ),
407 array(
408 'mode' => cache_store::MODE_SESSION,
409 'store' => 'default_session',
410 'sort' => -1
411 ),
412 array(
413 'mode' => cache_store::MODE_REQUEST,
414 'store' => 'default_request',
415 'sort' => -1
416 )
417 );
418 $writer->configlocks = array(
419 'default_file_lock' => array(
420 'name' => 'cachelock_file_default',
421 'type' => 'cachelock_file',
422 'dir' => 'filelocks',
423 'default' => true
424 )
425 );
426
427 return $writer->generate_configuration_array();
428 }
429
430 /**
431 * Updates the definition in the configuration from those found in the cache files.
432 *
433 * @param bool $coreonly Unused.
434 */
435 public static function update_definitions($coreonly = false) {
436 // Nothing to do here.
437 }
438
439 /**
440 * Locates all of the definition files.
441 *
442 * @param bool $coreonly Unused.
443 * @return array
444 */
445 protected static function locate_definitions($coreonly = false) {
446 return array();
447 }
448
449 /**
450 * Sets the mappings for a given definition.
451 *
452 * @param string $definition Unused.
453 * @param array $mappings Unused.
454 * @throws coding_exception
455 */
456 public function set_definition_mappings($definition, $mappings) {
457 // Nothing to do here.
458 }
459}