Skip to content

Commit

Permalink
MDL-51111 cache: Don't lock and write the config file if unchanged
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanheywood committed Feb 11, 2021
1 parent 4e398ff commit 05f1386
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cache/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public static function instance() {
*/
protected function config_save() {
global $CFG;
static $confighash = '';
$cachefile = static::get_config_file_path();
$directory = dirname($cachefile);
if ($directory !== $CFG->dataroot && !file_exists($directory)) {
Expand All @@ -86,6 +87,15 @@ protected function config_save() {
// Prepare the file content.
$content = "<?php defined('MOODLE_INTERNAL') || die();\n \$configuration = ".var_export($configuration, true).";";

// Do both file content and hash based detection because this might be called
// many times within a single request.
$hash = sha1($content);
if (($hash === $confighash) || (file_exists($cachefile) && $content === file_get_contents($cachefile))) {
// Config is unchanged so don't bother locking and writing.
$confighash = $hash;
return;
}

// We need to create a temporary cache lock instance for use here. Remember we are generating the config file
// it doesn't exist and thus we can't use the normal API for this (it'll just try to use config).
$lockconf = reset($this->configlocks);
Expand Down

0 comments on commit 05f1386

Please sign in to comment.