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 // Nothing to do here.
56 * Gets a key from the cache.
58 * @param int|string $key
59 * @param int $strictness Unused.
62 public function get($key, $strictness = IGNORE_MISSING
) {
67 * Gets many keys at once from the cache.
70 * @param int $strictness Unused.
73 public function get_many(array $keys, $strictness = IGNORE_MISSING
) {
75 foreach ($keys as $key) {
76 $return[$key] = false;
82 * Sets a key value pair in the cache.
84 * @param int|string $key Unused.
85 * @param mixed $data Unused.
88 public function set($key, $data) {
93 * Sets many key value pairs in the cache at once.
95 * @param array $keyvaluearray Unused.
98 public function set_many(array $keyvaluearray) {
103 * Deletes an item from the cache.
105 * @param int|string $key Unused.
106 * @param bool $recurse Unused.
109 public function delete($key, $recurse = true) {
114 * Deletes many items at once from the cache.
116 * @param array $keys Unused.
117 * @param bool $recurse Unused.
120 public function delete_many(array $keys, $recurse = true) {
125 * Checks if the cache has the requested key.
127 * @param int|string $key Unused.
130 public function has($key) {
135 * Checks if the cache has all of the requested keys.
136 * @param array $keys Unused.
139 public function has_all(array $keys) {
144 * Checks if the cache has any of the requested keys.
146 * @param array $keys Unused.
149 public function has_any(array $keys) {
154 * Purges all items from the cache.
158 public function purge() {
164 * The cache factory class used when the Cache has been disabled.
166 * @copyright 2012 Sam Hemelryk
167 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
169 class cache_factory_disabled
extends cache_factory
{
172 * Returns an instance of the cache_factor method.
174 * @param bool $forcereload Unused.
175 * @return cache_factory
176 * @throws coding_exception
178 public static function instance($forcereload = false) {
179 throw new coding_exception('You must not call to this cache factory within your code.');
183 * Creates a definition instance or returns the existing one if it has already been created.
185 * @param string $component
186 * @param string $area
187 * @param string $unused Used to be datasourceaggregate but that was removed and this is now unused.
188 * @return cache_definition
190 public function create_definition($component, $area, $unused = null) {
191 return cache_definition
::load_adhoc(cache_store
::MODE_REQUEST
, $component, $area);
195 * Common public method to create a cache instance given a definition.
197 * @param cache_definition $definition
198 * @return cache_application|cache_session|cache_store
199 * @throws coding_exception
201 public function create_cache(cache_definition
$definition) {
202 return new cache_disabled($definition, $this->create_dummy_store($definition));
206 * Creates a cache object given the parameters for a definition.
208 * @param string $component
209 * @param string $area
210 * @param array $identifiers
211 * @param string $unused Used to be datasourceaggregate but that was removed and this is now unused.
212 * @return cache_application|cache_session|cache_request
214 public function create_cache_from_definition($component, $area, array $identifiers = array(), $unused = null) {
215 // Regular cache definitions are cached inside create_definition(). This is not the case for disabledlib.php
216 // definitions as they use load_adhoc(). They are built as a new object on each call.
217 // We do not need to clone the definition because we know it's new.
218 $definition = $this->create_definition($component, $area);
219 $definition->set_identifiers($identifiers);
220 $cache = $this->create_cache($definition);
225 * Creates an ad-hoc cache from the given param.
228 * @param string $component
229 * @param string $area
230 * @param array $identifiers
231 * @param array $options An array of options, available options are:
232 * - simplekeys : Set to true if the keys you will use are a-zA-Z0-9_
233 * - simpledata : Set to true if the type of the data you are going to store is scalar, or an array of scalar vars
234 * - staticacceleration : If set to true the cache will hold onto all data passing through it.
235 * - staticaccelerationsize : Sets the max size of the static acceleration array.
236 * @return cache_application|cache_session|cache_request
238 public function create_cache_from_params($mode, $component, $area, array $identifiers = array(), array $options = array()) {
239 // Regular cache definitions are cached inside create_definition(). This is not the case for disabledlib.php
240 // definitions as they use load_adhoc(). They are built as a new object on each call.
241 // We do not need to clone the definition because we know it's new.
242 $definition = cache_definition
::load_adhoc($mode, $component, $area, $options);
243 $definition->set_identifiers($identifiers);
244 $cache = $this->create_cache($definition);
249 * Creates a store instance given its name and configuration.
251 * @param string $name Unused.
252 * @param array $details Unused.
253 * @param cache_definition $definition
254 * @return boolean|cache_store
256 public function create_store_from_config($name, array $details, cache_definition
$definition) {
257 return $this->create_dummy_store($definition);
261 * Creates a cache config instance with the ability to write if required.
263 * @param bool $writer Unused.
264 * @return cache_config_disabled|cache_config_writer
266 public function create_config_instance($writer = false) {
267 // We are always going to use the cache_config_disabled class for all regular request.
268 // However if the code has requested the writer then likely something is changing and
269 // we're going to need to interact with the config.php file.
270 // In this case we will still use the cache_config_writer.
271 $class = 'cache_config_disabled';
273 // If the writer was requested then something is changing.
274 $class = 'cache_config_writer';
276 if (!array_key_exists($class, $this->configs
)) {
277 self
::set_state(self
::STATE_INITIALISING
);
278 if ($class === 'cache_config_disabled') {
279 $configuration = $class::create_default_configuration();
281 $configuration = false;
282 if (!cache_config
::config_file_exists()) {
283 cache_config_writer
::create_default_configuration(true);
286 $this->configs
[$class] = new $class;
287 $this->configs
[$class]->load($configuration);
289 self
::set_state(self
::STATE_READY
);
291 // Return the instance.
292 return $this->configs
[$class];
297 * The cache config class used when the Cache has been disabled.
299 * @copyright 2012 Sam Hemelryk
300 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
302 class cache_config_disabled
extends cache_config_writer
{
305 * Returns an instance of the configuration writer.
307 * @return cache_config_disabled
309 public static function instance() {
310 $factory = cache_factory
::instance();
311 return $factory->create_config_instance(true);
315 * Saves the current configuration.
317 protected function config_save() {
318 // Nothing to do here.
322 * Generates a configuration array suitable to be written to the config file.
326 protected function generate_configuration_array() {
327 $configuration = array();
328 $configuration['stores'] = $this->configstores
;
329 $configuration['modemappings'] = $this->configmodemappings
;
330 $configuration['definitions'] = $this->configdefinitions
;
331 $configuration['definitionmappings'] = $this->configdefinitionmappings
;
332 $configuration['locks'] = $this->configlocks
;
333 return $configuration;
337 * Adds a plugin instance.
339 * @param string $name Unused.
340 * @param string $plugin Unused.
341 * @param array $configuration Unused.
343 * @throws cache_exception
345 public function add_store_instance($name, $plugin, array $configuration = array()) {
350 * Sets the mode mappings.
352 * @param array $modemappings Unused.
354 * @throws cache_exception
356 public function set_mode_mappings(array $modemappings) {
361 * Edits a give plugin instance.
363 * @param string $name Unused.
364 * @param string $plugin Unused.
365 * @param array $configuration Unused.
367 * @throws cache_exception
369 public function edit_store_instance($name, $plugin, $configuration) {
374 * Deletes a store instance.
376 * @param string $name Unused.
378 * @throws cache_exception
380 public function delete_store_instance($name) {
385 * Creates the default configuration and saves it.
387 * @param bool $forcesave Ignored because we are disabled!
390 public static function create_default_configuration($forcesave = false) {
394 // We probably need to come up with a better way to create the default stores, or at least ensure 100% that the
395 // default store plugins are protected from deletion.
396 require_once($CFG->dirroot
.'/cache/stores/file/lib.php');
397 require_once($CFG->dirroot
.'/cache/stores/session/lib.php');
398 require_once($CFG->dirroot
.'/cache/stores/static/lib.php');
401 $writer->configstores
= array(
402 'default_application' => array(
403 'name' => 'default_application',
405 'configuration' => array(),
406 'features' => cachestore_file
::get_supported_features(),
407 'modes' => cache_store
::MODE_APPLICATION
,
410 'default_session' => array(
411 'name' => 'default_session',
412 'plugin' => 'session',
413 'configuration' => array(),
414 'features' => cachestore_session
::get_supported_features(),
415 'modes' => cache_store
::MODE_SESSION
,
418 'default_request' => array(
419 'name' => 'default_request',
420 'plugin' => 'static',
421 'configuration' => array(),
422 'features' => cachestore_static
::get_supported_features(),
423 'modes' => cache_store
::MODE_REQUEST
,
427 $writer->configdefinitions
= array();
428 $writer->configmodemappings
= array(
430 'mode' => cache_store
::MODE_APPLICATION
,
431 'store' => 'default_application',
435 'mode' => cache_store
::MODE_SESSION
,
436 'store' => 'default_session',
440 'mode' => cache_store
::MODE_REQUEST
,
441 'store' => 'default_request',
445 $writer->configlocks
= array(
446 'default_file_lock' => array(
447 'name' => 'cachelock_file_default',
448 'type' => 'cachelock_file',
449 'dir' => 'filelocks',
454 return $writer->generate_configuration_array();
458 * Updates the definition in the configuration from those found in the cache files.
460 * @param bool $coreonly Unused.
462 public static function update_definitions($coreonly = false) {
463 // Nothing to do here.
467 * Locates all of the definition files.
469 * @param bool $coreonly Unused.
472 protected static function locate_definitions($coreonly = false) {
477 * Sets the mappings for a given definition.
479 * @param string $definition Unused.
480 * @param array $mappings Unused.
481 * @throws coding_exception
483 public function set_definition_mappings($definition, $mappings) {
484 // Nothing to do here.