1441 |
ariadna |
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 |
namespace core_cache;
|
|
|
18 |
|
|
|
19 |
use cachestore_static;
|
|
|
20 |
use core\exception\coding_exception;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* The cache factory class used when the Cache has been disabled.
|
|
|
24 |
*
|
|
|
25 |
* @package core_cache
|
|
|
26 |
* @copyright 2012 Sam Hemelryk
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
*/
|
|
|
29 |
class disabled_factory extends factory {
|
|
|
30 |
/** @var array Array of temporary caches in use. */
|
|
|
31 |
protected static $tempcaches = [];
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Returns an instance of the factory method.
|
|
|
35 |
*
|
|
|
36 |
* @param bool $forcereload Unused.
|
|
|
37 |
* @return factory
|
|
|
38 |
* @throws coding_exception
|
|
|
39 |
*/
|
|
|
40 |
public static function instance($forcereload = false) {
|
|
|
41 |
throw new coding_exception('You must not call to this cache factory within your code.');
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Creates a definition instance or returns the existing one if it has already been created.
|
|
|
46 |
*
|
|
|
47 |
* @param string $component
|
|
|
48 |
* @param string $area
|
|
|
49 |
* @param string $unused Used to be datasourceaggregate but that was removed and this is now unused.
|
|
|
50 |
* @return definition
|
|
|
51 |
*/
|
|
|
52 |
public function create_definition($component, $area, $unused = null) {
|
|
|
53 |
$definition = parent::create_definition($component, $area);
|
|
|
54 |
if ($definition->has_data_source()) {
|
|
|
55 |
return $definition;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
return definition::load_adhoc(store::MODE_REQUEST, $component, $area);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Common public method to create a cache instance given a definition.
|
|
|
63 |
*
|
|
|
64 |
* @param definition $definition
|
|
|
65 |
* @return application_cache|session_cache|store
|
|
|
66 |
* @throws coding_exception
|
|
|
67 |
*/
|
|
|
68 |
public function create_cache(definition $definition) {
|
|
|
69 |
$loader = null;
|
|
|
70 |
if ($definition->has_data_source()) {
|
|
|
71 |
$loader = $definition->get_data_source();
|
|
|
72 |
}
|
|
|
73 |
return new disabled_cache($definition, $this->create_dummy_store($definition), $loader);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Creates a cache object given the parameters for a definition.
|
|
|
78 |
*
|
|
|
79 |
* @param string $component
|
|
|
80 |
* @param string $area
|
|
|
81 |
* @param array $identifiers
|
|
|
82 |
* @param string $unused Used to be datasourceaggregate but that was removed and this is now unused.
|
|
|
83 |
* @return application_cache|session_cache|request_cache
|
|
|
84 |
*/
|
|
|
85 |
public function create_cache_from_definition($component, $area, array $identifiers = [], $unused = null) {
|
|
|
86 |
// Temporary in-memory caches are sometimes allowed when caching is disabled.
|
|
|
87 |
if (\core_cache\allow_temporary_caches::is_allowed() && !$identifiers) {
|
|
|
88 |
$key = $component . '/' . $area;
|
|
|
89 |
if (array_key_exists($key, self::$tempcaches)) {
|
|
|
90 |
$cache = self::$tempcaches[$key];
|
|
|
91 |
} else {
|
|
|
92 |
$definition = $this->create_definition($component, $area);
|
|
|
93 |
// The cachestore_static class returns true to all three 'SUPPORTS_' checks so it
|
|
|
94 |
// can be used with all definitions.
|
|
|
95 |
$store = new cachestore_static('TEMP:' . $component . '/' . $area);
|
|
|
96 |
$store->initialise($definition);
|
|
|
97 |
// We need to use a cache loader wrapper rather than directly returning the store,
|
|
|
98 |
// or it wouldn't have support for versioning. The application_cache class is used
|
|
|
99 |
// (rather than request_cache which might make more sense logically) because it
|
|
|
100 |
// includes support for locking, which might be necessary for some caches.
|
|
|
101 |
$cache = new application_cache($definition, $store);
|
|
|
102 |
self::$tempcaches[$key] = $cache;
|
|
|
103 |
}
|
|
|
104 |
return $cache;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
// Regular cache definitions are cached inside create_definition(). This is not the case for disabledlib.php
|
|
|
108 |
// definitions as they use load_adhoc(). They are built as a new object on each call.
|
|
|
109 |
// We do not need to clone the definition because we know it's new.
|
|
|
110 |
$definition = $this->create_definition($component, $area);
|
|
|
111 |
$definition->set_identifiers($identifiers);
|
|
|
112 |
$cache = $this->create_cache($definition);
|
|
|
113 |
return $cache;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* Removes all temporary caches.
|
|
|
118 |
*
|
|
|
119 |
* Don't call this directly - used by {@see \core_cache\allow_temporary_caches}.
|
|
|
120 |
*/
|
|
|
121 |
public static function clear_temporary_caches(): void {
|
|
|
122 |
self::$tempcaches = [];
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
/**
|
|
|
126 |
* Creates an ad-hoc cache from the given param.
|
|
|
127 |
*
|
|
|
128 |
* @param int $mode
|
|
|
129 |
* @param string $component
|
|
|
130 |
* @param string $area
|
|
|
131 |
* @param array $identifiers
|
|
|
132 |
* @param array $options An array of options, available options are:
|
|
|
133 |
* - simplekeys : Set to true if the keys you will use are a-zA-Z0-9_
|
|
|
134 |
* - simpledata : Set to true if the type of the data you are going to store is scalar, or an array of scalar vars
|
|
|
135 |
* - staticacceleration : If set to true the cache will hold onto all data passing through it.
|
|
|
136 |
* - staticaccelerationsize : Sets the max size of the static acceleration array.
|
|
|
137 |
* @return application_cache|session_cache|request_cache
|
|
|
138 |
*/
|
|
|
139 |
public function create_cache_from_params($mode, $component, $area, array $identifiers = [], array $options = []) {
|
|
|
140 |
// Regular cache definitions are cached inside create_definition(). This is not the case for disabledlib.php
|
|
|
141 |
// definitions as they use load_adhoc(). They are built as a new object on each call.
|
|
|
142 |
// We do not need to clone the definition because we know it's new.
|
|
|
143 |
$definition = definition::load_adhoc($mode, $component, $area, $options);
|
|
|
144 |
$definition->set_identifiers($identifiers);
|
|
|
145 |
$cache = $this->create_cache($definition);
|
|
|
146 |
return $cache;
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* Creates a store instance given its name and configuration.
|
|
|
151 |
*
|
|
|
152 |
* @param string $name Unused.
|
|
|
153 |
* @param array $details Unused.
|
|
|
154 |
* @param definition $definition
|
|
|
155 |
* @return boolean|store
|
|
|
156 |
*/
|
|
|
157 |
public function create_store_from_config($name, array $details, definition $definition) {
|
|
|
158 |
return $this->create_dummy_store($definition);
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
/**
|
|
|
162 |
* Creates a cache config instance with the ability to write if required.
|
|
|
163 |
*
|
|
|
164 |
* @param bool $writer Unused.
|
|
|
165 |
* @return disabled_config|config_writer
|
|
|
166 |
*/
|
|
|
167 |
public function create_config_instance($writer = false) {
|
|
|
168 |
// We are always going to use the disabled_config class for all regular request.
|
|
|
169 |
// However if the code has requested the writer then likely something is changing and
|
|
|
170 |
// we're going to need to interact with the config.php file.
|
|
|
171 |
// In this case we will still use the config_writer.
|
|
|
172 |
$class = disabled_config::class;
|
|
|
173 |
if ($writer) {
|
|
|
174 |
// If the writer was requested then something is changing.
|
|
|
175 |
$class = config_writer::class;
|
|
|
176 |
}
|
|
|
177 |
if (!array_key_exists($class, $this->configs)) {
|
|
|
178 |
self::set_state(factory::STATE_INITIALISING);
|
|
|
179 |
if ($class === disabled_config::class) {
|
|
|
180 |
$configuration = $class::create_default_configuration();
|
|
|
181 |
$this->configs[$class] = new $class();
|
|
|
182 |
} else {
|
|
|
183 |
$configuration = false;
|
|
|
184 |
// If we need a writer, we should get the classname from the generic factory.
|
|
|
185 |
// This is so alternative classes can be used if a different writer is required.
|
|
|
186 |
$this->configs[$class] = parent::get_disabled_writer();
|
|
|
187 |
}
|
|
|
188 |
$this->configs[$class]->load($configuration);
|
|
|
189 |
}
|
|
|
190 |
self::set_state(factory::STATE_READY);
|
|
|
191 |
|
|
|
192 |
// Return the instance.
|
|
|
193 |
return $this->configs[$class];
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
/**
|
|
|
197 |
* Returns true if the cache API has been disabled.
|
|
|
198 |
*
|
|
|
199 |
* @return bool
|
|
|
200 |
*/
|
|
|
201 |
public function is_disabled() {
|
|
|
202 |
return true;
|
|
|
203 |
}
|
|
|
204 |
}
|
|
|
205 |
// Alias this class to the old name.
|
|
|
206 |
// This file will be autoloaded by the legacyclasses autoload system.
|
|
|
207 |
// In future all uses of this class will be corrected and the legacy references will be removed.
|
|
|
208 |
class_alias(disabled_factory::class, \cache_factory_disabled::class);
|