From ee9fa046a55634849567fa41096dc0ece01e25ea Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Fri, 18 Feb 2022 10:31:37 +0200 Subject: [PATCH] Do not update index on timeout if no need --- .../Grav/Common/Page/Medium/AbstractMedia.php | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/system/src/Grav/Common/Page/Medium/AbstractMedia.php b/system/src/Grav/Common/Page/Medium/AbstractMedia.php index 5b3ef6f4e..4e639f16b 100644 --- a/system/src/Grav/Common/Page/Medium/AbstractMedia.php +++ b/system/src/Grav/Common/Page/Medium/AbstractMedia.php @@ -566,6 +566,8 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac $list[$filename] = $info; } + ksort($list, SORT_NATURAL); + return $list; } @@ -640,13 +642,20 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac // Get file media listing. Use cached version if possible to avoid I/O. if (null === $this->index) { $now = time(); - [$files, $timestamp] = $this->loadIndex(); + [$data, $timestamp] = $this->loadIndex(); $timeout = $this->indexTimeout; if (!$timestamp || ($timeout && $timestamp < $now - $timeout)) { $media_types = $config->get('media.types'); - $files = $this->prepareFileInfo($this->loadFileInfo(), $media_types, $files); - - $this->saveIndex($files, $now); + $files = $this->prepareFileInfo($this->loadFileInfo(), $media_types, $data['files'] ?? []); + $oldChecksum = $data['checksum'] ?? null; + $newChecksum = md5(serialize($files)); + if ($oldChecksum !== $newChecksum) { + $this->saveIndex($files, $now); + } else { + $this->touchIndex($now); + } + } else { + $files = $data['files'] ?? []; } $this->index = $files; @@ -802,7 +811,7 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac // Read media index file. $indexFile = $this->getIndexFile(); if (!($indexFile && $indexFile->exists())) { - return [[], 0, 0]; + return [[], 0]; } $index = $indexFile->load(); @@ -810,23 +819,24 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac $folder = $index['folder'] ?? null; $type = $index['type'] ?? null; if ($version !== static::VERSION || $folder !== $this->path || $type !== ($this->config['type'] ?? 'local')) { - return [[], 0, 0]; + return [[], 0]; } - return [$index['files'] ?? [], $index['timestamp'] ?? 0, $indexFile->getModificationTime()]; + return [$index, $indexFile->getModificationTime()]; } /** + * @param int|null $timestamp * @return void */ - protected function touchIndex(): void + protected function touchIndex(?int $timestamp = null): void { $index = $this->getIndexFile(); if (!$index || !$this->exists) { return; } - $index->touch(); + $index->touch($timestamp); } /** @@ -834,7 +844,7 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac * @param int|null $timestamp * @return void */ - protected function saveIndex(array $files, ?int $timestamp = null): void + protected function saveIndex(array $files, string $checksum, ?int $timestamp = null): void { $index = $this->getIndexFile(); if (!$index || !$this->exists) { @@ -844,6 +854,7 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac $data = [ 'type' => $this->config['type'] ?? 'local', 'version' => static::VERSION, + 'checksum' => $checksum, 'timestamp' => $timestamp ?? time(), 'folder' => $this->path, 'url' => $this->url,