Skip to content

Commit

Permalink
Merge branch 'MDL-71014-cache-siteid' of https://github.com/brendanhe…
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Sep 2, 2021
2 parents 443a980 + 7b93808 commit 6ae9f22
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
3 changes: 2 additions & 1 deletion lib/accesslib.php
Expand Up @@ -6291,7 +6291,8 @@ public static function instance($instanceid = 0, $strictness = MUST_EXIST, $cach
debugging('context_system::instance(): invalid $id parameter detected, should be 0');
}

if (defined('SYSCONTEXTID') and $cache) { // dangerous: define this in config.php to eliminate 1 query/page
// SYSCONTEXTID is cached in local cache to eliminate 1 query per page.
if (defined('SYSCONTEXTID') and $cache) {
if (!isset(context::$systemcontext)) {
$record = new stdClass();
$record->id = SYSCONTEXTID;
Expand Down
17 changes: 6 additions & 11 deletions lib/moodlelib.php
Expand Up @@ -1457,8 +1457,6 @@ function set_config($name, $value, $plugin=null) {
*
* NOTE: this function is called from lib/db/upgrade.php
*
* @static string|false $siteidentifier The site identifier is not cached. We use this static cache so
* that we need only fetch it once per request.
* @param string $plugin full component name
* @param string $name default null
* @return mixed hash-like object or single value, return false no config found
Expand All @@ -1467,8 +1465,6 @@ function set_config($name, $value, $plugin=null) {
function get_config($plugin, $name = null) {
global $CFG, $DB;

static $siteidentifier = null;

if ($plugin === 'moodle' || $plugin === 'core' || empty($plugin)) {
$forced =& $CFG->config_php_settings;
$iscore = true;
Expand All @@ -1482,12 +1478,11 @@ function get_config($plugin, $name = null) {
$iscore = false;
}

if ($siteidentifier === null) {
if (!isset($CFG->siteidentifier)) {
try {
// This may fail during installation.
// If you have a look at {@link initialise_cfg()} you will see that this is how we detect the need to
// install the database.
$siteidentifier = $DB->get_field('config', 'value', array('name' => 'siteidentifier'));
// This may throw an exception during installation, which is how we detect the
// need to install the database. For more details see {@see initialise_cfg()}.
$CFG->siteidentifier = $DB->get_field('config', 'value', array('name' => 'siteidentifier'));
} catch (dml_exception $ex) {
// Set siteidentifier to false. We don't want to trip this continually.
$siteidentifier = false;
Expand All @@ -1499,7 +1494,7 @@ function get_config($plugin, $name = null) {
if (array_key_exists($name, $forced)) {
return (string)$forced[$name];
} else if ($name === 'siteidentifier' && $plugin == 'core') {
return $siteidentifier;
return $CFG->siteidentifier;
}
}

Expand All @@ -1524,7 +1519,7 @@ function get_config($plugin, $name = null) {
}

if ($plugin === 'core') {
$result['siteidentifier'] = $siteidentifier;
$result['siteidentifier'] = $CFG->siteidentifier;
}

foreach ($forced as $key => $value) {
Expand Down
17 changes: 15 additions & 2 deletions lib/setup.php
Expand Up @@ -651,6 +651,17 @@
unset($dbhash);
}

// Load any immutable bootstrap config from local cache.
$bootstrapcachefile = $CFG->localcachedir . '/bootstrap.php';
if (is_readable($bootstrapcachefile)) {
try {
require_once($bootstrapcachefile);
} catch (Throwable $e) {
// If it is corrupted then attempt to delete it and it will be rebuilt.
@unlink($bootstrapcachefile);
}
}

// Load up any configuration from the config table or MUC cache.
if (PHPUNIT_TEST) {
phpunit_util::initialise_cfg();
Expand Down Expand Up @@ -752,8 +763,7 @@
// initialise ME's - this must be done BEFORE starting of session!
initialise_fullme();

// define SYSCONTEXTID in config.php if you want to save some queries,
// after install it must match the system context record id.
// SYSCONTEXTID is cached in local cache to eliminate 1 query per page.
if (!defined('SYSCONTEXTID')) {
context_system::instance();
}
Expand Down Expand Up @@ -1056,6 +1066,9 @@
$PAGE = new moodle_page();
}

// Cache any immutable config locally to avoid constant DB lookups.
initialise_local_config_cache();

// Allow plugins to callback as soon possible after setup.php is loaded.
$pluginswithfunction = get_plugins_with_function('after_config', 'lib.php');
foreach ($pluginswithfunction as $plugins) {
Expand Down
24 changes: 24 additions & 0 deletions lib/setuplib.php
Expand Up @@ -795,6 +795,30 @@ function initialise_cfg() {
}
}

/**
* Cache any immutable config locally to avoid constant DB lookups.
*
* Only to be used only from lib/setup.php
*/
function initialise_local_config_cache() {
global $CFG;

$bootstrapcachefile = $CFG->localcachedir . '/bootstrap.php';

if (!empty($CFG->siteidentifier) && !file_exists($bootstrapcachefile)) {
$contents = "<?php
// ********** This file is generated DO NOT EDIT **********
\$CFG->siteidentifier = '" . addslashes($CFG->siteidentifier) . "';
define('SYSCONTEXTID', ".SYSCONTEXTID.");
";

$temp = $bootstrapcachefile . '.tmp' . uniqid();
file_put_contents($temp, $contents);
@chmod($temp, $CFG->filepermissions);
rename($temp, $bootstrapcachefile);
}
}

/**
* Initialises $FULLME and friends. Private function. Should only be called from
* setup.php.
Expand Down

0 comments on commit 6ae9f22

Please sign in to comment.