2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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.
25 * @copyright 2012 Sam Hemelryk
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') ||
die();
32 * Required as it is needed for cache_config_disabled which extends cache_config_writer.
34 require_once($CFG->dirroot
.'/cache/locallib.php');
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
{
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 if ($loader instanceof cache_data_source
) {
53 // Set the data source to allow data sources to work when caching is entirely disabled.
54 $this->set_data_source($loader);
57 // No other features are handled.
61 * Gets a key from the cache.
63 * @param int|string $key
64 * @param int $strictness Unused.
67 public function get($key, $strictness = IGNORE_MISSING
) {
68 if ($this->get_datasource() !== false) {
69 return $this->get_datasource()->load_for_cache($key);
76 * Gets many keys at once from the cache.
79 * @param int $strictness Unused.
82 public function get_many(array $keys, $strictness = IGNORE_MISSING
) {
83 if ($this->get_datasource() !== false) {
84 return $this->get_datasource()->load_many_for_cache($keys);
87 return array_combine($keys, array_fill(0, count($keys), false));
91 * Sets a key value pair in the cache.
93 * @param int|string $key Unused.
94 * @param mixed $data Unused.
97 public function set($key, $data) {
102 * Sets many key value pairs in the cache at once.
104 * @param array $keyvaluearray Unused.
107 public function set_many(array $keyvaluearray) {
112 * Deletes an item from the cache.
114 * @param int|string $key Unused.
115 * @param bool $recurse Unused.
118 public function delete($key, $recurse = true) {
123 * Deletes many items at once from the cache.
125 * @param array $keys Unused.
126 * @param bool $recurse Unused.
129 public function delete_many(array $keys, $recurse = true) {
134 * Checks if the cache has the requested key.
136 * @param int|string $key Unused.
137 * @param bool $tryloadifpossible Unused.
140 public function has($key, $tryloadifpossible = false) {
141 $result = $this->get($key);
143 return $result !== false;
147 * Checks if the cache has all of the requested keys.
148 * @param array $keys Unused.
151 public function has_all(array $keys) {
152 if (!$this->get_datasource()) {
156 foreach ($keys as $key) {
157 if (!$this->has($key)) {
165 * Checks if the cache has any of the requested keys.
167 * @param array $keys Unused.
170 public function has_any(array $keys) {
171 foreach ($keys as $key) {
172 if ($this->has($key)) {
181 * Purges all items from the cache.
185 public function purge() {
191 * The cache factory class used when the Cache has been disabled.
193 * @copyright 2012 Sam Hemelryk
194 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
196 class cache_factory_disabled
extends cache_factory
{
199 * Returns an instance of the cache_factor method.
201 * @param bool $forcereload Unused.
202 * @return cache_factory
203 * @throws coding_exception
205 public static function instance($forcereload = false) {
206 throw new coding_exception('You must not call to this cache factory within your code.');
210 * Creates a definition instance or returns the existing one if it has already been created.
212 * @param string $component
213 * @param string $area
214 * @param string $unused Used to be datasourceaggregate but that was removed and this is now unused.
215 * @return cache_definition
217 public function create_definition($component, $area, $unused = null) {
218 $definition = parent
::create_definition($component, $area);
219 if ($definition->has_data_source()) {
223 return cache_definition
::load_adhoc(cache_store
::MODE_REQUEST
, $component, $area);
227 * Common public method to create a cache instance given a definition.
229 * @param cache_definition $definition
230 * @return cache_application|cache_session|cache_store
231 * @throws coding_exception
233 public function create_cache(cache_definition
$definition) {
235 if ($definition->has_data_source()) {
236 $loader = $definition->get_data_source();
238 return new cache_disabled($definition, $this->create_dummy_store($definition), $loader);
242 * Creates a cache object given the parameters for a definition.
244 * @param string $component
245 * @param string $area
246 * @param array $identifiers
247 * @param string $unused Used to be datasourceaggregate but that was removed and this is now unused.
248 * @return cache_application|cache_session|cache_request
250 public function create_cache_from_definition($component, $area, array $identifiers = array(), $unused = null) {
251 // Regular cache definitions are cached inside create_definition(). This is not the case for disabledlib.php
252 // definitions as they use load_adhoc(). They are built as a new object on each call.
253 // We do not need to clone the definition because we know it's new.
254 $definition = $this->create_definition($component, $area);
255 $definition->set_identifiers($identifiers);
256 $cache = $this->create_cache($definition);
261 * Creates an ad-hoc cache from the given param.
264 * @param string $component
265 * @param string $area
266 * @param array $identifiers
267 * @param array $options An array of options, available options are:
268 * - simplekeys : Set to true if the keys you will use are a-zA-Z0-9_
269 * - simpledata : Set to true if the type of the data you are going to store is scalar, or an array of scalar vars
270 * - staticacceleration : If set to true the cache will hold onto all data passing through it.
271 * - staticaccelerationsize : Sets the max size of the static acceleration array.
272 * @return cache_application|cache_session|cache_request
274 public function create_cache_from_params($mode, $component, $area, array $identifiers = array(), array $options = array()) {
275 // Regular cache definitions are cached inside create_definition(). This is not the case for disabledlib.php
276 // definitions as they use load_adhoc(). They are built as a new object on each call.
277 // We do not need to clone the definition because we know it's new.
278 $definition = cache_definition
::load_adhoc($mode, $component, $area, $options);
279 $definition->set_identifiers($identifiers);
280 $cache = $this->create_cache($definition);
285 * Creates a store instance given its name and configuration.
287 * @param string $name Unused.
288 * @param array $details Unused.
289 * @param cache_definition $definition
290 * @return boolean|cache_store
292 public function create_store_from_config($name, array $details, cache_definition
$definition) {
293 return $this->create_dummy_store($definition);
297 * Creates a cache config instance with the ability to write if required.
299 * @param bool $writer Unused.
300 * @return cache_config_disabled|cache_config_writer
302 public function create_config_instance($writer = false) {
303 // We are always going to use the cache_config_disabled class for all regular request.
304 // However if the code has requested the writer then likely something is changing and
305 // we're going to need to interact with the config.php file.
306 // In this case we will still use the cache_config_writer.
307 $class = 'cache_config_disabled';
309 // If the writer was requested then something is changing.
310 $class = 'cache_config_writer';
312 if (!array_key_exists($class, $this->configs
)) {
313 self
::set_state(self
::STATE_INITIALISING
);
314 if ($class === 'cache_config_disabled') {
315 $configuration = $class::create_default_configuration();
317 $configuration = false;
318 if (!cache_config
::config_file_exists()) {
319 cache_config_writer
::create_default_configuration(true);
322 $this->configs
[$class] = new $class;
323 $this->configs
[$class]->load($configuration);
325 self
::set_state(self
::STATE_READY
);
327 // Return the instance.
328 return $this->configs
[$class];
332 * Returns true if the cache API has been disabled.
336 public function is_disabled() {
342 * The cache config class used when the Cache has been disabled.
344 * @copyright 2012 Sam Hemelryk
345 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
347 class cache_config_disabled
extends cache_config_writer
{
350 * Returns an instance of the configuration writer.
352 * @return cache_config_disabled
354 public static function instance() {
355 $factory = cache_factory
::instance();
356 return $factory->create_config_instance(true);
360 * Saves the current configuration.
362 protected function config_save() {
363 // Nothing to do here.
367 * Generates a configuration array suitable to be written to the config file.
371 protected function generate_configuration_array() {
372 $configuration = array();
373 $configuration['stores'] = $this->configstores
;
374 $configuration['modemappings'] = $this->configmodemappings
;
375 $configuration['definitions'] = $this->configdefinitions
;
376 $configuration['definitionmappings'] = $this->configdefinitionmappings
;
377 $configuration['locks'] = $this->configlocks
;
378 return $configuration;
382 * Adds a plugin instance.
384 * @param string $name Unused.
385 * @param string $plugin Unused.
386 * @param array $configuration Unused.
388 * @throws cache_exception
390 public function add_store_instance($name, $plugin, array $configuration = array()) {
395 * Sets the mode mappings.
397 * @param array $modemappings Unused.
399 * @throws cache_exception
401 public function set_mode_mappings(array $modemappings) {
406 * Edits a give plugin instance.
408 * @param string $name Unused.
409 * @param string $plugin Unused.
410 * @param array $configuration Unused.
412 * @throws cache_exception
414 public function edit_store_instance($name, $plugin, $configuration) {
419 * Deletes a store instance.
421 * @param string $name Unused.
423 * @throws cache_exception
425 public function delete_store_instance($name) {
430 * Creates the default configuration and saves it.
432 * @param bool $forcesave Ignored because we are disabled!
435 public static function create_default_configuration($forcesave = false) {
439 // We probably need to come up with a better way to create the default stores, or at least ensure 100% that the
440 // default store plugins are protected from deletion.
441 require_once($CFG->dirroot
.'/cache/stores/file/lib.php');
442 require_once($CFG->dirroot
.'/cache/stores/session/lib.php');
443 require_once($CFG->dirroot
.'/cache/stores/static/lib.php');
446 $writer->configstores
= array(
447 'default_application' => array(
448 'name' => 'default_application',
450 'configuration' => array(),
451 'features' => cachestore_file
::get_supported_features(),
452 'modes' => cache_store
::MODE_APPLICATION
,
455 'default_session' => array(
456 'name' => 'default_session',
457 'plugin' => 'session',
458 'configuration' => array(),
459 'features' => cachestore_session
::get_supported_features(),
460 'modes' => cache_store
::MODE_SESSION
,
463 'default_request' => array(
464 'name' => 'default_request',
465 'plugin' => 'static',
466 'configuration' => array(),
467 'features' => cachestore_static
::get_supported_features(),
468 'modes' => cache_store
::MODE_REQUEST
,
472 $writer->configdefinitions
= array();
473 $writer->configmodemappings
= array(
475 'mode' => cache_store
::MODE_APPLICATION
,
476 'store' => 'default_application',
480 'mode' => cache_store
::MODE_SESSION
,
481 'store' => 'default_session',
485 'mode' => cache_store
::MODE_REQUEST
,
486 'store' => 'default_request',
490 $writer->configlocks
= array(
491 'default_file_lock' => array(
492 'name' => 'cachelock_file_default',
493 'type' => 'cachelock_file',
494 'dir' => 'filelocks',
499 return $writer->generate_configuration_array();
503 * Updates the definition in the configuration from those found in the cache files.
505 * @param bool $coreonly Unused.
507 public static function update_definitions($coreonly = false) {
508 // Nothing to do here.
512 * Locates all of the definition files.
514 * @param bool $coreonly Unused.
517 protected static function locate_definitions($coreonly = false) {
522 * Sets the mappings for a given definition.
524 * @param string $definition Unused.
525 * @param array $mappings Unused.
526 * @throws coding_exception
528 public function set_definition_mappings($definition, $mappings) {
529 // Nothing to do here.