Skip to content

Commit

Permalink
[project-base] reduced image url redis cache size (#2761)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasLudvik authored Sep 7, 2023
1 parent 636f350 commit 2f1fd9e
Showing 1 changed file with 16 additions and 34 deletions.
50 changes: 16 additions & 34 deletions app/src/Component/Image/ImageFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,24 @@ public function getImageUrl(
?string $type = null,
): string {
$image = $this->getImageByObject($imageOrEntity, $type);
$cacheId = $this->getCacheIdForImageUrl($image->getId(), $domainConfig->getId(), $type, $sizeName);
$cacheId = $this->getCacheIdForImageUrl($image->getId(), $domainConfig->getId());

return $this->cache->get(
$friendlyUrlSeoEntityName = $this->cache->get(
$cacheId,
function () use ($image, $domainConfig, $sizeName) {
function () use ($image, $domainConfig) {
if (!$this->imageLocator->imageExists($image)) {
throw new ImageNotFoundException();
}

$seoEntityName = $this->getSeoNameByImageAndLocale($image, $domainConfig->getLocale());
$friendlyUrlSeoEntityName = $this->getFriendlyUrlSlug($seoEntityName);

return $this->cdnFacade->resolveDomainUrlForAssets($domainConfig)
. $this->imageUrlPrefix
. $this->imageLocator->getRelativeImageFilepathWithSlug($image, $sizeName, $friendlyUrlSeoEntityName);
return $this->getFriendlyUrlSlug($seoEntityName);
},
);

return $this->cdnFacade->resolveDomainUrlForAssets($domainConfig)
. $this->imageUrlPrefix
. $this->imageLocator->getRelativeImageFilepathWithSlug($image, $sizeName, $friendlyUrlSeoEntityName);
}

/**
Expand Down Expand Up @@ -270,26 +271,24 @@ protected function getAdditionalImageUrl(
$cacheId = $this->getCacheIdForImageUrl(
$image->getId(),
$domainConfig->getId(),
$image->getType(),
$sizeName,
$additionalSizeIndex,
);

return $this->cache->get(
$friendlyUrlSeoEntityName = $this->cache->get(
$cacheId,
function () use ($image, $domainConfig, $additionalSizeIndex, $sizeName) {
function () use ($image, $domainConfig) {
if (!$this->imageLocator->imageExists($image)) {
throw new ImageNotFoundException();
}

$seoEntityName = $this->getSeoNameByImageAndLocale($image, $domainConfig->getLocale());
$friendlyUrlSeoEntityName = $this->getFriendlyUrlSlug($seoEntityName);

return $this->cdnFacade->resolveDomainUrlForAssets($domainConfig)
. $this->imageUrlPrefix
. $this->imageLocator->getRelativeAdditionalImageFilepathWithSlug($image, $additionalSizeIndex, $sizeName, $friendlyUrlSeoEntityName);
return $this->getFriendlyUrlSlug($seoEntityName);
},
);

return $this->cdnFacade->resolveDomainUrlForAssets($domainConfig)
. $this->imageUrlPrefix
. $this->imageLocator->getRelativeAdditionalImageFilepathWithSlug($image, $additionalSizeIndex, $sizeName, $friendlyUrlSeoEntityName);
}

/**
Expand Down Expand Up @@ -411,36 +410,19 @@ protected function saveImageOrdering($orderedImages): void
parent::saveImageOrdering($persistedImages);
}

/**
* @return bool
*/
public function clearImageCache(): bool
{
return $this->cache->clear();
}

/**
* @param int $imageId
* @param int $domainId
* @param string|null $type
* @param string|null $sizeName
* @param int|null $additionalIndex
* @return string
*/
private function getCacheIdForImageUrl(
int $imageId,
int $domainId,
?string $type,
?string $sizeName,
?int $additionalIndex = null,
): string {
return sprintf(
'ImageUrl_imageId-%d_domainId-%d_type-%s_size-%s_additionalIndex-%s',
'ImageUrl_imageId-%d_domainId-%d',
$imageId,
$domainId,
$type,
$sizeName,
$additionalIndex,
);
}
}

0 comments on commit 2f1fd9e

Please sign in to comment.