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 24, 2021
1 parent 713b28c commit af3e51f
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 af3e51f

Please sign in to comment.