From 71aaba751425ed026b2c2bb005833436a1fcbce3 Mon Sep 17 00:00:00 2001 From: Sebastian Helzle Date: Tue, 10 Oct 2023 09:14:38 +0200 Subject: [PATCH] BUGFIX: Load all thumbnails for an asset to skip further requests For the usecase of images with responsive variants this change prevents additional database requests for each additional variant of an image. --- Neos.Media/Classes/Domain/Model/Thumbnail.php | 5 +++++ .../Classes/Domain/Service/ThumbnailService.php | 12 ++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Neos.Media/Classes/Domain/Model/Thumbnail.php b/Neos.Media/Classes/Domain/Model/Thumbnail.php index bbc5ed5552c..56d3cf87c82 100644 --- a/Neos.Media/Classes/Domain/Model/Thumbnail.php +++ b/Neos.Media/Classes/Domain/Model/Thumbnail.php @@ -224,4 +224,9 @@ public function refresh() { $this->generatorStrategy->refresh($this); } + + public function getConfigurationHash(): string + { + return $this->configurationHash; + } } diff --git a/Neos.Media/Classes/Domain/Service/ThumbnailService.php b/Neos.Media/Classes/Domain/Service/ThumbnailService.php index 4efc5544fd4..32843c971c3 100644 --- a/Neos.Media/Classes/Domain/Service/ThumbnailService.php +++ b/Neos.Media/Classes/Domain/Service/ThumbnailService.php @@ -132,8 +132,16 @@ public function getThumbnail(AssetInterface $asset, ThumbnailConfiguration $conf if (isset($this->thumbnailCache[$assetIdentifier][$configurationHash])) { $thumbnail = $this->thumbnailCache[$assetIdentifier][$configurationHash]; } else { - $thumbnail = $this->thumbnailRepository->findOneByAssetAndThumbnailConfiguration($asset, $configuration); - $this->thumbnailCache[$assetIdentifier][$configurationHash] = $thumbnail; + $thumbnail = null; + // Load all thumbnails for the asset and cache them to prevent further db requests for the same asset + $thumbnailsForAsset = $this->thumbnailRepository->findByOriginalAsset($asset); + /** @var Thumbnail $thumbnailVariant */ + foreach ($thumbnailsForAsset as $thumbnailVariant) { + $this->thumbnailCache[$assetIdentifier][$thumbnailVariant->getConfigurationHash()] = $thumbnailVariant; + if ($thumbnailVariant->getConfigurationHash() === $configurationHash) { + $thumbnail = $thumbnailVariant; + } + } } $async = $configuration->isAsync(); if ($thumbnail === null) {