Skip to content

Commit

Permalink
Fixed missing changes in yaml & markdown files if saved multiple time…
Browse files Browse the repository at this point in the history
…s during the same second because of a caching issue
  • Loading branch information
mahagr committed Mar 29, 2022
1 parent e1ca3c2 commit 7c2b21f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,9 @@
# v1.7.33
## mm/dd/2022

1. [](#bugfix)
* Fixed missing changes in yaml & markdown files if saved multiple times during the same second because of a caching issue

# v1.7.32
## 03/28/2022

Expand Down
12 changes: 8 additions & 4 deletions system/src/Grav/Common/File/CompiledFile.php
Expand Up @@ -32,9 +32,10 @@ trait CompiledFile
public function content($var = null)
{
try {
$filename = $this->filename;
// If nothing has been loaded, attempt to get pre-compiled version of the file first.
if ($var === null && $this->raw === null && $this->content === null) {
$key = md5($this->filename);
$key = md5($filename);
$file = PhpFile::instance(CACHE_DIR . "compiled/files/{$key}{$this->extension}.php");

$modified = $this->modified();
Expand All @@ -48,13 +49,15 @@ public function content($var = null)

$class = get_class($this);

$size = filesize($filename);
$cache = $file->exists() ? $file->content() : null;

// Load real file if cache isn't up to date (or is invalid).
if (!isset($cache['@class'])
|| $cache['@class'] !== $class
|| $cache['modified'] !== $modified
|| $cache['filename'] !== $this->filename
|| ($cache['size'] ?? null) !== $size
|| $cache['filename'] !== $filename
) {
// Attempt to lock the file for writing.
try {
Expand All @@ -67,8 +70,9 @@ public function content($var = null)
$data = (array)$this->decode($this->raw());
$cache = [
'@class' => $class,
'filename' => $this->filename,
'filename' => $filename,
'modified' => $modified,
'size' => $size,
'data' => $data
];

Expand All @@ -89,7 +93,7 @@ public function content($var = null)
$this->content = $cache['data'];
}
} catch (Exception $e) {
throw new RuntimeException(sprintf('Failed to read %s: %s', Utils::basename($this->filename), $e->getMessage()), 500, $e);
throw new RuntimeException(sprintf('Failed to read %s: %s', Utils::basename($filename), $e->getMessage()), 500, $e);
}

return parent::content($var);
Expand Down

0 comments on commit 7c2b21f

Please sign in to comment.