Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 12... Línea 12...
12
// GNU General Public License for more details.
12
// GNU General Public License for more details.
13
//
13
//
14
// You should have received a copy of the GNU General Public License
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/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea 16... Línea -...
16
 
-
 
17
/**
-
 
18
 * This file contains the cache factory class.
-
 
19
 *
-
 
20
 * This file is part of Moodle's cache API, affectionately called MUC.
-
 
21
 * It contains the components that are requried in order to use caching.
-
 
22
 *
-
 
23
 * @package    core
16
 
24
 * @category   cache
-
 
25
 * @copyright  2012 Sam Hemelryk
-
 
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
Línea 27... Línea 17...
27
 */
17
namespace core_cache;
-
 
18
 
-
 
19
use core\exception\coding_exception;
Línea 28... Línea 20...
28
 
20
use cache_config_testing;
29
defined('MOODLE_INTERNAL') || die();
21
use cache_phpunit_factory;
30
 
22
 
31
/**
23
/**
32
 * The cache factory class.
24
 * The cache factory class.
33
 *
25
 *
34
 * This factory class is important because it stores instances of objects used by the cache API and returns them upon requests.
26
 * This factory class is important because it stores instances of objects used by the cache API and returns them upon requests.
35
 * This allows us to both reuse objects saving on overhead, and gives us an easy place to "reset" the cache API in situations that
27
 * This allows us to both reuse objects saving on overhead, and gives us an easy place to "reset" the cache API in situations that
36
 * we need such as unit testing.
28
 * we need such as unit testing.
-
 
29
 *
37
 *
30
 * @copyright  2012 Sam Hemelryk
38
 * @copyright  2012 Sam Hemelryk
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
40
 */
32
 * @package core_cache
41
class cache_factory {
33
 */
42
 
34
class factory {
43
    /** The cache has not been initialised yet. */
35
    /** The cache has not been initialised yet. */
44
    const STATE_UNINITIALISED = 0;
36
    const STATE_UNINITIALISED = 0;
Línea 56... Línea 48...
56
    const STATE_DISABLED = 10;
48
    const STATE_DISABLED = 10;
57
    /** The cache stores have been disabled */
49
    /** The cache stores have been disabled */
58
    const STATE_STORES_DISABLED = 11;
50
    const STATE_STORES_DISABLED = 11;
Línea 59... Línea 51...
59
 
51
 
60
    /**
52
    /**
61
     * An instance of the cache_factory class created upon the first request.
53
     * An instance of the factory class created upon the first request.
62
     * @var cache_factory
54
     * @var factory
63
     */
55
     */
Línea 64... Línea 56...
64
    protected static $instance;
56
    protected static $instance;
65
 
57
 
66
    /**
58
    /**
67
     * An array containing caches created for definitions
59
     * An array containing caches created for definitions
68
     * @var array
60
     * @var array
Línea 69... Línea 61...
69
     */
61
     */
70
    protected $cachesfromdefinitions = array();
62
    protected $cachesfromdefinitions = [];
71
 
63
 
72
    /**
64
    /**
73
     * Array of caches created by parameters, ad-hoc definitions will have been used.
65
     * Array of caches created by parameters, ad-hoc definitions will have been used.
Línea 74... Línea 66...
74
     * @var array
66
     * @var array
75
     */
67
     */
76
    protected $cachesfromparams = array();
68
    protected $cachesfromparams = [];
77
 
69
 
78
    /**
70
    /**
Línea 79... Línea 71...
79
     * An array of stores organised by definitions.
71
     * An array of stores organised by definitions.
80
     * @var array
72
     * @var array
81
     */
73
     */
82
    protected $definitionstores = array();
74
    protected $definitionstores = [];
83
 
75
 
Línea 84... Línea 76...
84
    /**
76
    /**
85
     * An array of instantiated stores.
77
     * An array of instantiated stores.
86
     * @var array
78
     * @var array
87
     */
79
     */
88
    protected $stores = array();
80
    protected $stores = [];
Línea 89... Línea 81...
89
 
81
 
90
    /**
82
    /**
91
     * An array of configuration instances
83
     * An array of configuration instances
92
     * @var array
84
     * @var array
93
     */
85
     */
Línea 94... Línea 86...
94
    protected $configs = array();
86
    protected $configs = [];
95
 
87
 
96
    /**
88
    /**
97
     * An array of initialised definitions
89
     * An array of initialised definitions
98
     * @var array
90
     * @var array
Línea 99... Línea 91...
99
     */
91
     */
100
    protected $definitions = array();
92
    protected $definitions = [];
101
 
93
 
102
    /**
94
    /**
Línea 116... Línea 108...
116
     * @var core_cache\local\administration_display_helper
108
     * @var core_cache\local\administration_display_helper
117
     */
109
     */
118
    protected static $displayhelper = null;
110
    protected static $displayhelper = null;
Línea 119... Línea 111...
119
 
111
 
120
    /**
112
    /**
121
     * Returns an instance of the cache_factory class.
113
     * Returns an instance of the factory class.
122
     *
114
     *
123
     * @param bool $forcereload If set to true a new cache_factory instance will be created and used.
115
     * @param bool $forcereload If set to true a new factory instance will be created and used.
124
     * @return cache_factory
116
     * @return factory
125
     */
117
     */
126
    public static function instance($forcereload = false) {
118
    public static function instance($forcereload = false) {
127
        global $CFG;
119
        global $CFG;
128
        if ($forcereload || self::$instance === null) {
120
        if ($forcereload || self::$instance === null) {
129
            // Initialise a new factory to facilitate our needs.
121
            // Initialise a new factory to facilitate our needs.
130
            if (defined('CACHE_DISABLE_ALL') && CACHE_DISABLE_ALL !== false) {
122
            if (defined('CACHE_DISABLE_ALL') && CACHE_DISABLE_ALL !== false) {
131
                // The cache has been disabled. Load disabledlib and start using the factory designed to handle this
123
                // The cache has been disabled. Load disabledlib and start using the factory designed to handle this
132
                // situation. It will use disabled alternatives where available.
-
 
133
                require_once($CFG->dirroot.'/cache/disabledlib.php');
124
                // situation. It will use disabled alternatives where available.
134
                self::$instance = new cache_factory_disabled();
125
                self::$instance = new disabled_factory();
135
            } else if ((defined('PHPUNIT_TEST') && PHPUNIT_TEST) || defined('BEHAT_SITE_RUNNING')) {
126
            } else if ((defined('PHPUNIT_TEST') && PHPUNIT_TEST) || defined('BEHAT_SITE_RUNNING')) {
136
                // We're using the test factory.
127
                // We're using the test factory.
137
                require_once($CFG->dirroot.'/cache/tests/fixtures/lib.php');
128
                require_once($CFG->dirroot . '/cache/tests/fixtures/lib.php');
138
                self::$instance = new cache_phpunit_factory();
129
                self::$instance = new cache_phpunit_factory();
139
                if (defined('CACHE_DISABLE_STORES') && CACHE_DISABLE_STORES !== false) {
130
                if (defined('CACHE_DISABLE_STORES') && CACHE_DISABLE_STORES !== false) {
140
                    // The cache stores have been disabled.
131
                    // The cache stores have been disabled.
141
                    self::$instance->set_state(self::STATE_STORES_DISABLED);
132
                    self::$instance->set_state(self::STATE_STORES_DISABLED);
142
                }
-
 
143
 
133
                }
144
            } else if (!empty($CFG->alternative_cache_factory_class)) {
134
            } else if (!empty($CFG->alternative_cache_factory_class)) {
145
                $factoryclass = $CFG->alternative_cache_factory_class;
135
                $factoryclass = $CFG->alternative_cache_factory_class;
146
                self::$instance = new $factoryclass();
136
                self::$instance = new $factoryclass();
147
            } else {
137
            } else {
148
                // We're using the regular factory.
138
                // We're using the regular factory.
149
                self::$instance = new cache_factory();
139
                self::$instance = new factory();
150
                if (defined('CACHE_DISABLE_STORES') && CACHE_DISABLE_STORES !== false) {
140
                if (defined('CACHE_DISABLE_STORES') && CACHE_DISABLE_STORES !== false) {
151
                    // The cache stores have been disabled.
141
                    // The cache stores have been disabled.
152
                    self::$instance->set_state(self::STATE_STORES_DISABLED);
142
                    self::$instance->set_state(self::STATE_STORES_DISABLED);
153
                }
143
                }
Línea 167... Línea 157...
167
     * Resets the arrays containing instantiated caches, stores, and config instances.
157
     * Resets the arrays containing instantiated caches, stores, and config instances.
168
     */
158
     */
169
    public static function reset() {
159
    public static function reset() {
170
        $factory = self::instance();
160
        $factory = self::instance();
171
        $factory->reset_cache_instances();
161
        $factory->reset_cache_instances();
172
        $factory->configs = array();
162
        $factory->configs = [];
173
        $factory->definitions = array();
163
        $factory->definitions = [];
174
        $factory->definitionstores = array();
164
        $factory->definitionstores = [];
175
        $factory->lockplugins = array(); // MUST be null in order to force its regeneration.
165
        $factory->lockplugins = []; // MUST be null in order to force its regeneration.
176
        // Reset the state to uninitialised.
166
        // Reset the state to uninitialised.
177
        $factory->state = self::STATE_UNINITIALISED;
167
        $factory->state = self::STATE_UNINITIALISED;
178
    }
168
    }
Línea 179... Línea 169...
179
 
169
 
Línea 182... Línea 172...
182
     *
172
     *
183
     * Cache objects still held onto by the code that initialised them will remain as is
173
     * Cache objects still held onto by the code that initialised them will remain as is
184
     * however all future requests for a cache/store will lead to a new instance being re-initialised.
174
     * however all future requests for a cache/store will lead to a new instance being re-initialised.
185
     */
175
     */
186
    public function reset_cache_instances() {
176
    public function reset_cache_instances() {
187
        $this->cachesfromdefinitions = array();
177
        $this->cachesfromdefinitions = [];
188
        $this->cachesfromparams = array();
178
        $this->cachesfromparams = [];
189
        $this->stores = array();
179
        $this->stores = [];
190
    }
180
    }
Línea 191... Línea 181...
191
 
181
 
192
    /**
182
    /**
193
     * Creates a cache object given the parameters for a definition.
183
     * Creates a cache object given the parameters for a definition.
Línea 196... Línea 186...
196
     *
186
     *
197
     * @param string $component
187
     * @param string $component
198
     * @param string $area
188
     * @param string $area
199
     * @param array $identifiers
189
     * @param array $identifiers
200
     * @param string $unused Used to be data source aggregate however that was removed and this is now unused.
190
     * @param string $unused Used to be data source aggregate however that was removed and this is now unused.
201
     * @return cache_application|cache_session|cache_request
191
     * @return application_cache|session_cache|request_cache
202
     */
192
     */
203
    public function create_cache_from_definition($component, $area, array $identifiers = array(), $unused = null) {
193
    public function create_cache_from_definition($component, $area, array $identifiers = [], $unused = null) {
204
        $identifierstring = empty($identifiers) ? '' : '/'.http_build_query($identifiers);
194
        $identifierstring = empty($identifiers) ? '' : '/' . http_build_query($identifiers);
205
        $definitionname = $component.'/'.$area.$identifierstring;
195
        $definitionname = $component . '/' . $area . $identifierstring;
206
        if (isset($this->cachesfromdefinitions[$definitionname])) {
196
        if (isset($this->cachesfromdefinitions[$definitionname])) {
207
            $cache = $this->cachesfromdefinitions[$definitionname];
197
            $cache = $this->cachesfromdefinitions[$definitionname];
208
            return $cache;
198
            return $cache;
209
        }
199
        }
210
        $definition = $this->create_definition($component, $area);
200
        $definition = $this->create_definition($component, $area);
Línea 230... Línea 220...
230
     * @param array $options An array of options, available options are:
220
     * @param array $options An array of options, available options are:
231
     *   - simplekeys : Set to true if the keys you will use are a-zA-Z0-9_
221
     *   - simplekeys : Set to true if the keys you will use are a-zA-Z0-9_
232
     *   - simpledata : Set to true if the type of the data you are going to store is scalar, or an array of scalar vars
222
     *   - simpledata : Set to true if the type of the data you are going to store is scalar, or an array of scalar vars
233
     *   - staticacceleration : If set to true the cache will hold onto data passing through it.
223
     *   - staticacceleration : If set to true the cache will hold onto data passing through it.
234
     *   - staticaccelerationsize : The maximum number of items to hold onto for acceleration purposes.
224
     *   - staticaccelerationsize : The maximum number of items to hold onto for acceleration purposes.
235
     * @return cache_application|cache_session|cache_request
225
     * @return application_cache|session_cache|request_cache
236
     */
226
     */
237
    public function create_cache_from_params($mode, $component, $area, array $identifiers = array(), array $options = array()) {
227
    public function create_cache_from_params($mode, $component, $area, array $identifiers = [], array $options = []) {
238
        $identifierstring = empty($identifiers) ? '' : '_'.http_build_query($identifiers);
228
        $identifierstring = empty($identifiers) ? '' : '_' . http_build_query($identifiers);
239
        $key = "{$mode}_{$component}_{$area}{$identifierstring}";
229
        $key = "{$mode}_{$component}_{$area}{$identifierstring}";
240
        if (isset($this->cachesfromparams[$key])) {
230
        if (isset($this->cachesfromparams[$key])) {
241
            return $this->cachesfromparams[$key];
231
            return $this->cachesfromparams[$key];
242
        }
232
        }
243
        // Regular cache definitions are cached inside create_definition().  This is not the case for Adhoc definitions
233
        // Regular cache definitions are cached inside create_definition().  This is not the case for Adhoc definitions
244
        // using load_adhoc().  They are built as a new object on each call.
234
        // using load_adhoc().  They are built as a new object on each call.
245
        // We do not need to clone the definition because we know it's new.
235
        // We do not need to clone the definition because we know it's new.
246
        $definition = cache_definition::load_adhoc($mode, $component, $area, $options);
236
        $definition = definition::load_adhoc($mode, $component, $area, $options);
247
        $definition->set_identifiers($identifiers);
237
        $definition->set_identifiers($identifiers);
248
        $cache = $this->create_cache($definition);
238
        $cache = $this->create_cache($definition);
249
        $this->cachesfromparams[$key] = $cache;
239
        $this->cachesfromparams[$key] = $cache;
250
        return $cache;
240
        return $cache;
251
    }
241
    }
Línea 253... Línea 243...
253
    /**
243
    /**
254
     * Common public method to create a cache instance given a definition.
244
     * Common public method to create a cache instance given a definition.
255
     *
245
     *
256
     * This is used by the static make methods.
246
     * This is used by the static make methods.
257
     *
247
     *
258
     * @param cache_definition $definition
248
     * @param definition $definition
259
     * @return cache_application|cache_session|cache_store
249
     * @return application_cache|session_cache|store
260
     * @throws coding_exception
250
     * @throws coding_exception
261
     */
251
     */
262
    public function create_cache(cache_definition $definition) {
252
    public function create_cache(definition $definition) {
263
        $class = $definition->get_cache_class();
253
        $class = $definition->get_cache_class();
264
        $stores = cache_helper::get_stores_suitable_for_definition($definition);
254
        $stores = helper::get_stores_suitable_for_definition($definition);
265
        foreach ($stores as $key => $store) {
255
        foreach ($stores as $key => $store) {
266
            if (!$store::are_requirements_met()) {
256
            if (!$store::are_requirements_met()) {
267
                unset($stores[$key]);
257
                unset($stores[$key]);
268
            }
258
            }
269
        }
259
        }
Línea 286... Línea 276...
286
     *
276
     *
287
     * If the store has already been instantiated then the original object will be returned. (reused)
277
     * If the store has already been instantiated then the original object will be returned. (reused)
288
     *
278
     *
289
     * @param string $name The name of the store (must be unique remember)
279
     * @param string $name The name of the store (must be unique remember)
290
     * @param array $details
280
     * @param array $details
291
     * @param cache_definition $definition The definition to instantiate it for.
281
     * @param definition $definition The definition to instantiate it for.
292
     * @return boolean|cache_store
282
     * @return boolean|store
293
     */
283
     */
294
    public function create_store_from_config($name, array $details, cache_definition $definition) {
284
    public function create_store_from_config($name, array $details, definition $definition) {
295
        if (!array_key_exists($name, $this->stores)) {
285
        if (!array_key_exists($name, $this->stores)) {
296
            // Properties: name, plugin, configuration, class.
286
            // Properties: name, plugin, configuration, class.
297
            $class = $details['class'];
287
            $class = $details['class'];
298
            if (!$class::are_requirements_met()) {
288
            if (!$class::are_requirements_met()) {
299
                return false;
289
                return false;
300
            }
290
            }
301
            $store = new $class($details['name'], $details['configuration']);
291
            $store = new $class($details['name'], $details['configuration']);
302
            $this->stores[$name] = $store;
292
            $this->stores[$name] = $store;
303
        }
293
        }
304
        /* @var cache_store $store */
294
        /* @var store $store */
305
        $store = $this->stores[$name];
295
        $store = $this->stores[$name];
306
        // We check are_requirements_met although we expect is_ready is going to check as well.
296
        // We check are_requirements_met although we expect is_ready is going to check as well.
307
        if (!$store::are_requirements_met() || !$store->is_ready() || !$store->is_supported_mode($definition->get_mode())) {
297
        if (!$store::are_requirements_met() || !$store->is_ready() || !$store->is_supported_mode($definition->get_mode())) {
308
            return false;
298
            return false;
309
        }
299
        }
Línea 315... Línea 305...
315
        // order to address the issues.
305
        // order to address the issues.
316
        $store = $this->stores[$name]->create_clone($details);
306
        $store = $this->stores[$name]->create_clone($details);
317
        $store->initialise($definition);
307
        $store->initialise($definition);
318
        $definitionid = $definition->get_id();
308
        $definitionid = $definition->get_id();
319
        if (!isset($this->definitionstores[$definitionid])) {
309
        if (!isset($this->definitionstores[$definitionid])) {
320
            $this->definitionstores[$definitionid] = array();
310
            $this->definitionstores[$definitionid] = [];
321
        }
311
        }
322
        $this->definitionstores[$definitionid][] = $store;
312
        $this->definitionstores[$definitionid][] = $store;
323
        return $store;
313
        return $store;
324
    }
314
    }
Línea 325... Línea 315...
325
 
315
 
326
    /**
316
    /**
327
     * Returns an array of cache stores that have been initialised for use in definitions.
317
     * Returns an array of cache stores that have been initialised for use in definitions.
328
     * @param cache_definition $definition
318
     * @param definition $definition
329
     * @return array
319
     * @return array
330
     */
320
     */
331
    public function get_store_instances_in_use(cache_definition $definition) {
321
    public function get_store_instances_in_use(definition $definition) {
332
        $id = $definition->get_id();
322
        $id = $definition->get_id();
333
        if (!isset($this->definitionstores[$id])) {
323
        if (!isset($this->definitionstores[$id])) {
334
            return array();
324
            return [];
335
        }
325
        }
336
        return $this->definitionstores[$id];
326
        return $this->definitionstores[$id];
Línea 337... Línea 327...
337
    }
327
    }
Línea 346... Línea 336...
346
    }
336
    }
Línea 347... Línea 337...
347
 
337
 
348
    /**
338
    /**
349
     * Gets all adhoc caches that have been used within this request.
339
     * Gets all adhoc caches that have been used within this request.
350
     *
340
     *
351
     * @return cache_store[] Caches currently in use
341
     * @return store[] Caches currently in use
352
     */
342
     */
353
    public function get_adhoc_caches_in_use() {
343
    public function get_adhoc_caches_in_use() {
354
        return $this->cachesfromparams;
344
        return $this->cachesfromparams;
Línea 355... Línea 345...
355
    }
345
    }
356
 
346
 
357
    /**
347
    /**
358
     * Creates a cache config instance with the ability to write if required.
348
     * Creates a cache config instance with the ability to write if required.
359
     *
349
     *
360
     * @param bool $writer If set to true an instance that can update the configuration will be returned.
350
     * @param bool $writer If set to true an instance that can update the configuration will be returned.
361
     * @return cache_config|cache_config_writer
351
     * @return config|config_writer
362
     */
352
     */
Línea 363... Línea 353...
363
    public function create_config_instance($writer = false) {
353
    public function create_config_instance($writer = false) {
364
        global $CFG;
354
        global $CFG;
365
 
355
 
366
        // The class to use.
356
        // The class to use.
Línea 367... Línea 357...
367
        $class = 'cache_config';
357
        $class = config::class;
368
        // Are we running tests of some form?
358
        // Are we running tests of some form?
369
        $testing = (defined('PHPUNIT_TEST') && PHPUNIT_TEST) || defined('BEHAT_SITE_RUNNING');
-
 
370
 
359
        $testing = (defined('PHPUNIT_TEST') && PHPUNIT_TEST) || defined('BEHAT_SITE_RUNNING');
371
        // Check if this is a PHPUnit test and redirect to the phpunit config classes if it is.
360
 
372
        if ($testing) {
361
        // Check if this is a PHPUnit test and redirect to the phpunit config classes if it is.
373
            require_once($CFG->dirroot.'/cache/locallib.php');
362
        if ($testing) {
374
            require_once($CFG->dirroot.'/cache/tests/fixtures/lib.php');
363
            require_once($CFG->dirroot . '/cache/tests/fixtures/lib.php');
375
            // We have just a single class for PHP unit tests. We don't care enough about its
364
            // We have just a single class for PHP unit tests. We don't care enough about its
Línea 376... Línea 365...
376
            // performance to do otherwise and having a single method allows us to inject things into it
365
            // performance to do otherwise and having a single method allows us to inject things into it
377
            // while testing.
366
            // while testing.
Línea 378... Línea 367...
378
            $class = 'cache_config_testing';
367
            $class = cache_config_testing::class;
379
        }
-
 
380
 
368
        }
381
        // Check if we need to create a config file with defaults.
369
 
382
        $needtocreate = !$class::config_file_exists();
370
        // Check if we need to create a config file with defaults.
383
 
371
        $needtocreate = !$class::config_file_exists();
Línea 384... Línea 372...
384
        if ($writer || $needtocreate) {
372
 
385
            require_once($CFG->dirroot.'/cache/locallib.php');
373
        if ($writer || $needtocreate) {
386
            if (!$testing) {
374
            if (!$testing) {
387
                $class .= '_writer';
375
                $class .= '_writer';
388
            }
376
            }
389
        }
377
        }
390
 
378
 
391
        $error = false;
379
        $error = false;
392
        if ($needtocreate) {
380
        if ($needtocreate) {
393
            // Create the default configuration.
381
            // Create the default configuration.
394
            // Update the state, we are now initialising the cache.
382
            // Update the state, we are now initialising the cache.
395
            self::set_state(self::STATE_INITIALISING);
383
            self::set_state(self::STATE_INITIALISING);
396
            /** @var cache_config_writer $class */
384
            /** @var config_writer $class */
397
            $configuration = $class::create_default_configuration();
385
            $configuration = $class::create_default_configuration();
398
            if ($configuration !== true) {
386
            if ($configuration !== true) {
Línea 399... Línea 387...
399
                // Failed to create the default configuration. Disable the cache stores and update the state.
387
                // Failed to create the default configuration. Disable the cache stores and update the state.
400
                self::set_state(self::STATE_ERROR_INITIALISING);
388
                self::set_state(self::STATE_ERROR_INITIALISING);
401
                $this->configs[$class] = new $class;
389
                $this->configs[$class] = new $class();
402
                $this->configs[$class]->load($configuration);
390
                $this->configs[$class]->load($configuration);
403
                $error = true;
391
                $error = true;
Línea 404... Línea 392...
404
            }
392
            }
405
        }
393
        }
Línea 423... Línea 411...
423
     * Creates a definition instance or returns the existing one if it has already been created.
411
     * Creates a definition instance or returns the existing one if it has already been created.
424
     * @param string $component
412
     * @param string $component
425
     * @param string $area
413
     * @param string $area
426
     * @param string $unused This used to be data source aggregate - however that functionality has been removed and
414
     * @param string $unused This used to be data source aggregate - however that functionality has been removed and
427
     *        this argument is now unused.
415
     *        this argument is now unused.
428
     * @return cache_definition
416
     * @return definition
429
     * @throws coding_exception If the definition cannot be found.
417
     * @throws coding_exception If the definition cannot be found.
430
     */
418
     */
431
    public function create_definition($component, $area, $unused = null) {
419
    public function create_definition($component, $area, $unused = null) {
432
        $id = $component.'/'.$area;
420
        $id = $component . '/' . $area;
433
        if (!isset($this->definitions[$id])) {
421
        if (!isset($this->definitions[$id])) {
434
            // This is the first time this definition has been requested.
422
            // This is the first time this definition has been requested.
435
            if ($this->is_initialising()) {
423
            if ($this->is_initialising()) {
436
                // We're initialising the cache right now. Don't try to create another config instance.
424
                // We're initialising the cache right now. Don't try to create another config instance.
437
                // We'll just use an ad-hoc cache for the time being.
425
                // We'll just use an ad-hoc cache for the time being.
438
                $definition = cache_definition::load_adhoc(cache_store::MODE_REQUEST, $component, $area);
426
                $definition = definition::load_adhoc(store::MODE_REQUEST, $component, $area);
439
            } else {
427
            } else {
440
                // Load all the known definitions and find the desired one.
428
                // Load all the known definitions and find the desired one.
441
                $instance = $this->create_config_instance();
429
                $instance = $this->create_config_instance();
442
                $definition = $instance->get_definition_by_id($id);
430
                $definition = $instance->get_definition_by_id($id);
443
                if (!$definition) {
431
                if (!$definition) {
Línea 448... Línea 436...
448
                    if ($this->is_updating()) {
436
                    if ($this->is_updating()) {
449
                        // The cache is presently initialising and the requested cache definition has not been found.
437
                        // The cache is presently initialising and the requested cache definition has not been found.
450
                        // This means that the cache initialisation has requested something from a cache (I had recursive nightmares about this).
438
                        // This means that the cache initialisation has requested something from a cache (I had recursive nightmares about this).
451
                        // To serve this purpose and avoid errors we are going to make use of an ad-hoc cache rather than
439
                        // To serve this purpose and avoid errors we are going to make use of an ad-hoc cache rather than
452
                        // search for the definition which would possibly cause an infitite loop trying to initialise the cache.
440
                        // search for the definition which would possibly cause an infitite loop trying to initialise the cache.
453
                        $definition = cache_definition::load_adhoc(cache_store::MODE_REQUEST, $component, $area);
441
                        $definition = definition::load_adhoc(store::MODE_REQUEST, $component, $area);
454
                    } else {
442
                    } else {
455
                        // Either a typo of the developer has just created the definition and is using it for the first time.
443
                        // Either a typo of the developer has just created the definition and is using it for the first time.
456
                        $this->reset();
444
                        $this->reset();
457
                        $instance = $this->create_config_instance(true);
445
                        $instance = $this->create_config_instance(true);
458
                        $instance->update_definitions();
446
                        $instance->update_definitions();
459
                        $definition = $instance->get_definition_by_id($id);
447
                        $definition = $instance->get_definition_by_id($id);
460
                        if (!$definition) {
448
                        if (!$definition) {
461
                            throw new coding_exception('The requested cache definition does not exist.'. $id, $id);
449
                            throw new coding_exception('The requested cache definition does not exist.' . $id, $id);
462
                        }
450
                        }
463
                        if (!$this->is_disabled()) {
451
                        if (!$this->is_disabled()) {
464
                            debugging('Cache definitions reparsed causing cache reset in order to locate definition.
452
                            debugging('Cache definitions reparsed causing cache reset in order to locate definition.
465
                                You should bump the version number to ensure definitions are reprocessed.', DEBUG_DEVELOPER);
453
                                You should bump the version number to ensure definitions are reprocessed.', DEBUG_DEVELOPER);
466
                        }
454
                        }
467
                        $definition = cache_definition::load($id, $definition);
455
                        $definition = definition::load($id, $definition);
468
                    }
456
                    }
469
                } else {
457
                } else {
470
                    $definition = cache_definition::load($id, $definition);
458
                    $definition = definition::load($id, $definition);
471
                }
459
                }
472
            }
460
            }
473
            $this->definitions[$id] = $definition;
461
            $this->definitions[$id] = $definition;
474
        }
462
        }
475
        return $this->definitions[$id];
463
        return $this->definitions[$id];
476
    }
464
    }
Línea 477... Línea 465...
477
 
465
 
478
    /**
466
    /**
479
     * Creates a dummy store object for use when a loader has no potential stores to use.
467
     * Creates a dummy store object for use when a loader has no potential stores to use.
480
     *
468
     *
481
     * @param cache_definition $definition
469
     * @param definition $definition
482
     * @return cachestore_dummy
470
     * @return dummy_cachestore
483
     */
471
     */
484
    protected function create_dummy_store(cache_definition $definition) {
-
 
485
        global $CFG;
-
 
486
        require_once($CFG->dirroot.'/cache/classes/dummystore.php');
472
    protected function create_dummy_store(definition $definition) {
487
        $store = new cachestore_dummy();
473
        $store = new dummy_cachestore();
488
        $store->initialise($definition);
474
        $store->initialise($definition);
489
        return $store;
475
        return $store;
Línea 490... Línea 476...
490
    }
476
    }
491
 
477
 
492
    /**
478
    /**
493
     * Returns a lock instance ready for use.
479
     * Returns a lock instance ready for use.
494
     *
480
     *
495
     * @param array $config
481
     * @param array $config
496
     * @return cache_lock_interface
482
     * @return lockable_cache_interface
497
     */
483
     */
498
    public function create_lock_instance(array $config) {
484
    public function create_lock_instance(array $config) {
499
        global $CFG;
485
        global $CFG;
Línea 505... Línea 491...
505
        unset($config['name']);
491
        unset($config['name']);
506
        unset($config['type']);
492
        unset($config['type']);
Línea 507... Línea 493...
507
 
493
 
508
        if (!isset($this->lockplugins[$type])) {
494
        if (!isset($this->lockplugins[$type])) {
509
            $pluginname = substr($type, 10);
495
            $pluginname = substr($type, 10);
510
            $file = $CFG->dirroot."/cache/locks/{$pluginname}/lib.php";
496
            $file = $CFG->dirroot . "/cache/locks/{$pluginname}/lib.php";
511
            if (file_exists($file) && is_readable($file)) {
497
            if (file_exists($file) && is_readable($file)) {
512
                require_once($file);
498
                require_once($file);
513
            }
499
            }
514
            if (!class_exists($type)) {
500
            if (!class_exists($type)) {
Línea 609... Línea 595...
609
     * This function has been marked as protected so that it cannot be abused through the public API presently.
595
     * This function has been marked as protected so that it cannot be abused through the public API presently.
610
     * Perhaps in the future we will allow this, however as per the build up to the first release containing
596
     * Perhaps in the future we will allow this, however as per the build up to the first release containing
611
     * MUC it was decided that this was just to risky and abusable.
597
     * MUC it was decided that this was just to risky and abusable.
612
     */
598
     */
613
    protected static function disable() {
599
    protected static function disable() {
614
        global $CFG;
-
 
615
        require_once($CFG->dirroot.'/cache/disabledlib.php');
-
 
616
        self::$instance = new cache_factory_disabled();
600
        self::$instance = new disabled_factory();
617
    }
601
    }
Línea 618... Línea 602...
618
 
602
 
619
    /**
603
    /**
620
     * Returns true if the cache stores have been disabled.
604
     * Returns true if the cache stores have been disabled.
Línea 633... Línea 617...
633
     * This is useful in situations where you cannot be sure any stores are working.
617
     * This is useful in situations where you cannot be sure any stores are working.
634
     *
618
     *
635
     * In order to re-enable the cache you must call the cache factories static reset method:
619
     * In order to re-enable the cache you must call the cache factories static reset method:
636
     * <code>
620
     * <code>
637
     * // Disable the cache factory.
621
     * // Disable the cache factory.
638
     * cache_factory::disable_stores();
622
     * factory::disable_stores();
639
     * // Re-enable the cache factory by resetting it.
623
     * // Re-enable the cache factory by resetting it.
640
     * cache_factory::reset();
624
     * factory::reset();
641
     * </code>
625
     * </code>
642
     */
626
     */
643
    public static function disable_stores() {
627
    public static function disable_stores() {
644
        // First reset to clear any static acceleration array.
628
        // First reset to clear any static acceleration array.
645
        $factory = self::instance();
629
        $factory = self::instance();
Línea 648... Línea 632...
648
    }
632
    }
Línea 649... Línea 633...
649
 
633
 
650
    /**
634
    /**
651
     * Returns an instance of the current display_helper.
635
     * Returns an instance of the current display_helper.
652
     *
636
     *
653
     * @return core_cache\administration_helper
637
     * @return administration_helper
654
     */
638
     */
655
    public static function get_administration_display_helper(): core_cache\administration_helper {
639
    public static function get_administration_display_helper(): administration_helper {
656
        if (is_null(self::$displayhelper)) {
640
        if (is_null(self::$displayhelper)) {
657
            self::$displayhelper = new \core_cache\local\administration_display_helper();
641
            self::$displayhelper = new \core_cache\local\administration_display_helper();
658
        }
642
        }
659
        return self::$displayhelper;
643
        return self::$displayhelper;
Línea 660... Línea 644...
660
    }
644
    }
661
 
645
 
662
    /**
646
    /**
663
     * Gets the cache_config_writer to use when caching is disabled.
647
     * Gets the config_writer to use when caching is disabled.
664
     * This should only be called from cache_factory_disabled.
648
     * This should only be called from disabled_factory.
665
     *
649
     *
666
     * @return cache_config_writer
650
     * @return config_writer
667
     */
651
     */
Línea 668... Línea 652...
668
    public static function get_disabled_writer(): cache_config_writer {
652
    public static function get_disabled_writer(): config_writer {
669
        global $CFG;
653
        global $CFG;
670
 
654
 
Línea 678... Línea 662...
678
        if (!$loop && !empty($CFG->alternative_cache_factory_class)) {
662
        if (!$loop && !empty($CFG->alternative_cache_factory_class)) {
679
            // Get the class to use from the alternative factory.
663
            // Get the class to use from the alternative factory.
680
            $factoryinstance = new $CFG->alternative_cache_factory_class();
664
            $factoryinstance = new $CFG->alternative_cache_factory_class();
681
            return $factoryinstance::get_disabled_writer();
665
            return $factoryinstance::get_disabled_writer();
682
        } else {
666
        } else {
683
            // We got here from cache_factory_disabled.
667
            // We got here from disabled_factory.
684
            // We should use the default writer here.
668
            // We should use the default writer here.
685
            // Make sure we have a default config if needed.
669
            // Make sure we have a default config if needed.
686
            if (!cache_config::config_file_exists()) {
670
            if (!config::config_file_exists()) {
687
                cache_config_writer::create_default_configuration(true);
671
                config_writer::create_default_configuration(true);
688
            }
672
            }
Línea 689... Línea 673...
689
 
673
 
690
            return new cache_config_writer();
674
            return new config_writer();
691
        }
675
        }
692
    }
676
    }
-
 
677
}
-
 
678
 
-
 
679
// Alias this class to the old name.
-
 
680
// This file will be autoloaded by the legacyclasses autoload system.
-
 
681
// In future all uses of this class will be corrected and the legacy references will be removed.