Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MDL-36466 cache: tidy up after review

  • Loading branch information...
commit 94ef67cf5fad90b61a1ccc2918c7403b547bd63e 1 parent 3308259
Sam Hemelryk samhemelryk authored
6 admin/cli/install.php
@@ -127,6 +127,9 @@
127 127 /** Used by library scripts to check they are being called by Moodle */
128 128 define('MOODLE_INTERNAL', true);
129 129
  130 +// Disables caching.. just in case.
  131 +define('CACHE_DISABLE_ALL', true);
  132 +
130 133 // Check that PHP is of a sufficient version
131 134 if (version_compare(phpversion(), "5.3.2") < 0) {
132 135 $phpversion = phpversion();
@@ -169,9 +172,6 @@
169 172 require($CFG->dirroot.'/version.php');
170 173 $CFG->target_release = $release;
171 174
172   -// Disable the cache API.
173   -cache_factory::disable();
174   -
175 175 //Database types
176 176 $databases = array('mysqli' => moodle_database::get_driver_instance('mysqli', 'native'),
177 177 'pgsql' => moodle_database::get_driver_instance('pgsql', 'native'),
8 admin/index.php
@@ -105,9 +105,6 @@
105 105 }
106 106
107 107 if (!core_tables_exist()) {
108   - // Disable the Cache API as much as possible for installation.
109   - cache_factory::disable();
110   -
111 108 $PAGE->set_pagelayout('maintenance');
112 109 $PAGE->set_popup_notification_allowed(false);
113 110
@@ -197,9 +194,6 @@
197 194 if ($version > $CFG->version) { // upgrade
198 195 purge_all_caches();
199 196
200   - // Disable the Cache API as much as possible for upgrade.
201   - cache_factory::disable();
202   -
203 197 $PAGE->set_pagelayout('maintenance');
204 198 $PAGE->set_popup_notification_allowed(false);
205 199
@@ -304,8 +298,6 @@
304 298 }
305 299
306 300 if (moodle_needs_upgrading()) {
307   - // Disable the Cache API as much as possible for upgrade.
308   - cache_factory::disable();
309 301 if (!$PAGE->headerprinted) {
310 302 // means core upgrade or installation was not already done
311 303 if (!$confirmplugins) {
4 cache/README.md
Source Rendered
@@ -72,10 +72,10 @@ Disabling the cache entirely.
72 72 Like above there are times when you want the cache to avoid initialising anything it doesn't absolutely need. Things such as installation and upgrade require this functionality.
73 73 When the cache API is disabled it is still functional however special "disabled" classes will be used instead of the regular classes that make the Cache API tick.
74 74 These disabled classes do the least work possible and through this means we avoid all manner of intialisation and configuration.
75   -Once disabled its not recommened to reneable the Cache API. Instead if you need it, redirect.
  75 +Once disabled it cannot be re-enabled.
76 76
77 77 // To disable the cache entirely call the following:
78   - cache_factory::disable();
  78 + define('CACHE_DISABLE_ALL', true);
79 79
80 80 Cache API parts
81 81 ---------------
25 cache/classes/factory.php
@@ -110,11 +110,21 @@ class cache_factory {
110 110 * @return cache_factory
111 111 */
112 112 public static function instance($forcereload = false) {
  113 + global $CFG;
113 114 if ($forcereload || self::$instance === null) {
114   - self::$instance = new cache_factory();
115   - if (defined('CACHE_DISABLE_STORES') && CACHE_DISABLE_STORES !== false) {
116   - // The cache stores have been disabled.
117   - self::$instance->set_state(self::STATE_STORES_DISABLED);;
  115 + // Initialise a new factory to facilitate our needs.
  116 + if (defined('CACHE_DISABLE_ALL') && CACHE_DISABLE_ALL !== false) {
  117 + // The cache has been disabled. Load disabledlib and start using the factory designed to handle this
  118 + // situation. It will use disabled alternatives where available.
  119 + require_once($CFG->dirroot.'/cache/disabledlib.php');
  120 + self::$instance = new cache_factory_disabled();
  121 + } else {
  122 + // We're using the regular factory.
  123 + self::$instance = new cache_factory();
  124 + if (defined('CACHE_DISABLE_STORES') && CACHE_DISABLE_STORES !== false) {
  125 + // The cache stores have been disabled.
  126 + self::$instance->set_state(self::STATE_STORES_DISABLED);;
  127 + }
118 128 }
119 129 }
120 130 return self::$instance;
@@ -420,11 +430,14 @@ public function is_disabled() {
420 430 * In switching out the factory for the disabled factory it gains full control over the initialisation of objects
421 431 * and can use all of the disabled alternatives.
422 432 * Simple!
  433 + *
  434 + * This function has been marked as protected so that it cannot be abused through the public API presently.
  435 + * Perhaps in the future we will allow this, however as per the build up to the first release containing
  436 + * MUC it was decided that this was just to risky and abusable.
423 437 */
424   - public static function disable() {
  438 + protected static function disable() {
425 439 global $CFG;
426 440 require_once($CFG->dirroot.'/cache/disabledlib.php');
427   - self::$instance = null;
428 441 self::$instance = new cache_factory_disabled();
429 442 }
430 443
7 cache/disabledlib.php
@@ -225,10 +225,13 @@ public function create_cache_from_definition($component, $area, array $identifie
225 225 * @param string $component
226 226 * @param string $area
227 227 * @param array $identifiers
228   - * @param bool $persistent Unused.
  228 + * @param array $options An array of options, available options are:
  229 + * - simplekeys : Set to true if the keys you will use are a-zA-Z0-9_
  230 + * - simpledata : Set to true if the type of the data you are going to store is scalar, or an array of scalar vars
  231 + * - persistent : If set to true the cache will persist construction requests.
229 232 * @return cache_application|cache_session|cache_request
230 233 */
231   - public function create_cache_from_params($mode, $component, $area, array $identifiers = array(), $persistent = false) {
  234 + public function create_cache_from_params($mode, $component, $area, array $identifiers = array(), array $options = array()) {
232 235 $definition = cache_definition::load_adhoc($mode, $component, $area);
233 236 $cache = $this->create_cache($definition, $identifiers);
234 237 return $cache;
2  cache/tests/cache_test.php
@@ -714,7 +714,7 @@ public function test_disable() {
714 714 $this->assertTrue(@unlink($configfile));
715 715
716 716 // Disable the cache
717   - cache_factory::disable();
  717 + cache_phpunit_factory::phpunit_disable();
718 718
719 719 // Check we get the expected disabled factory.
720 720 $factory = cache_factory::instance();
17 cache/tests/fixtures/lib.php
@@ -216,4 +216,21 @@ public function phpunit_get_store_class() {
216 216 */
217 217 class cache_phpunit_dummy_overrideclass extends cache_application {
218 218 // Satisfying the code pre-checker is just part of my day job.
  219 +}
  220 +
  221 +/**
  222 + * Cache PHPUnit specific factory.
  223 + *
  224 + * @copyright 2012 Sam Hemelryk
  225 + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  226 + */
  227 +class cache_phpunit_factory extends cache_factory {
  228 + /**
  229 + * Exposes the cache_factory's disable method.
  230 + *
  231 + * Perhaps one day that method will be made public, for the time being it is protected.
  232 + */
  233 + public static function phpunit_disable() {
  234 + parent::disable();
  235 + }
219 236 }
4 install.php
@@ -45,6 +45,7 @@
45 45
46 46 define('CLI_SCRIPT', false); // prevents some warnings later
47 47 define('AJAX_SCRIPT', false); // prevents some warnings later
  48 +define('CACHE_DISABLE_ALL', true); // Disables caching.. just in case.
48 49
49 50 // Servers should define a default timezone in php.ini, but if they don't then make sure something is defined.
50 51 // This is a quick hack. Ideally we should ask the admin for a value. See MDL-22625 for more on this.
@@ -220,9 +221,6 @@
220 221 $hint_admindir = '';
221 222 $hint_database = '';
222 223
223   -// Disable the cache API.
224   -cache_factory::disable();
225   -
226 224 // Are we in help mode?
227 225 if (isset($_GET['help'])) {
228 226 install_print_help_page($_GET['help']);
8 lib/setup.php
@@ -138,6 +138,14 @@
138 138 define('PHPUNIT_TEST', false);
139 139 }
140 140
  141 +// When set to true MUC (Moodle caching) will be disabled as much as possible.
  142 +// A special cache factory will be used to handle this situation and will use special "disabled" equivalents objects.
  143 +// This ensure we don't attempt to read or create the config file, don't use stores, don't provide persistence or
  144 +// storage of any kind.
  145 +if (!defined('CACHE_DISABLE_ALL')) {
  146 + define('CACHE_DISABLE_ALL', false);
  147 +}
  148 +
141 149 // When set to true MUC (Moodle caching) will not use any of the defined or default stores.
142 150 // The Cache API will continue to function however this will force the use of the cachestore_dummy so all requests
143 151 // will be interacting with a static property and will never go to the proper cache stores.
21 lib/upgradelib.php
@@ -1419,6 +1419,10 @@ function install_core($version, $verbose) {
1419 1419 global $CFG, $DB;
1420 1420
1421 1421 try {
  1422 + // Disable the use of cache stores here. We will reset the factory after we've performed the installation.
  1423 + // This ensures that we don't permanently cache anything during installation.
  1424 + cache_factory::disable_stores();
  1425 +
1422 1426 set_time_limit(600);
1423 1427 print_upgrade_part_start('moodle', true, $verbose); // does not store upgrade running flag
1424 1428
@@ -1442,6 +1446,9 @@ function install_core($version, $verbose) {
1442 1446 admin_apply_default_settings(NULL, true);
1443 1447
1444 1448 print_upgrade_part_end(null, true, $verbose);
  1449 +
  1450 + // Reset the cache, this returns it to a normal operation state.
  1451 + cache_factory::reset();
1445 1452 } catch (exception $ex) {
1446 1453 upgrade_handle_exception($ex);
1447 1454 }
@@ -1463,6 +1470,9 @@ function upgrade_core($version, $verbose) {
1463 1470 try {
1464 1471 // Reset caches before any output
1465 1472 purge_all_caches();
  1473 + // Disable the use of cache stores here. We will reset the factory after we've performed the installation.
  1474 + // This ensures that we don't permanently cache anything during installation.
  1475 + cache_factory::disable_stores();
1466 1476
1467 1477 // Upgrade current language pack if we can
1468 1478 upgrade_language_pack();
@@ -1495,7 +1505,9 @@ function upgrade_core($version, $verbose) {
1495 1505 // Update core definitions.
1496 1506 cache_helper::update_definitions(true);
1497 1507
1498   - // Reset caches again, just to be sure
  1508 + // Reset the cache, this returns it to a normal operation state.
  1509 + cache_factory::reset();
  1510 + // Purge caches again, just to be sure we arn't holding onto old stuff now.
1499 1511 purge_all_caches();
1500 1512
1501 1513 // Clean up contexts - more and more stuff depends on existence of paths and contexts
@@ -1523,11 +1535,18 @@ function upgrade_noncore($verbose) {
1523 1535
1524 1536 // upgrade all plugins types
1525 1537 try {
  1538 + // Disable the use of cache stores here. We will reset the factory after we've performed the installation.
  1539 + // This ensures that we don't permanently cache anything during installation.
  1540 + cache_factory::disable_stores();
  1541 +
1526 1542 $plugintypes = get_plugin_types();
1527 1543 foreach ($plugintypes as $type=>$location) {
1528 1544 upgrade_plugins($type, 'print_upgrade_part_start', 'print_upgrade_part_end', $verbose);
1529 1545 }
  1546 + // Update cache definitions. Involves scanning each plugin for any changes.
1530 1547 cache_helper::update_definitions();
  1548 + // Reset the cache system to a normal state.
  1549 + cache_factory::reset();
1531 1550 } catch (Exception $ex) {
1532 1551 upgrade_handle_exception($ex);
1533 1552 }

0 comments on commit 94ef67c

Please sign in to comment.
Something went wrong with that request. Please try again.