Skip to content

Commit

Permalink
Reimplement background image cache to avoid cache warmup on deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
jskowronski39 committed Jan 8, 2024
1 parent 9ba4259 commit 9e01769
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 55 deletions.
14 changes: 9 additions & 5 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,17 @@ services:
$proxyKey: '%env(APP_IMGPROXY_KEY)%'
$proxySalt: '%env(APP_IMGPROXY_SALT)%'

app.cache.background_images:
class: Symfony\Component\Cache\Adapter\FilesystemAdapter
arguments:
$namespace: 'background_images'
$defaultLifetime: 3600
$directory: '%kernel.cache_dir%'

App\Twig\BackgroundImageExtension:
arguments:
$cacheDir: '%kernel.cache_dir%'
$cacheAdapter: '@app.cache.background_images'
$backgroundImagesDirectory: '%kernel.project_dir%/public/img/background'

App\Twig\UtilsExtension:
arguments:
Expand All @@ -106,7 +114,3 @@ services:
App\Form\DataTransformerRegistry:
arguments:
$registeredDataTransformers: !tagged_iterator app.form.registered_data_transformer

App\Cache\CacheWarmer\BackgroundImageCacheWarmer:
arguments:
$backgroundImagesDirectory: '%kernel.project_dir%/public/img/background'
2 changes: 0 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,3 @@ parameters:
container_xml_path: 'var/cache/dev/App_KernelDevDebugContainer.xml'
checkGenericClassInNonGenericObjectType: false
checkMissingIterableValueType: false
ignoreErrors:
- '#WarmableInterface::warmUp#'
44 changes: 0 additions & 44 deletions src/Cache/CacheWarmer/BackgroundImageCacheWarmer.php

This file was deleted.

23 changes: 19 additions & 4 deletions src/Twig/BackgroundImageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

namespace App\Twig;

use App\Cache\CacheWarmer\BackgroundImageCacheWarmer;
use Symfony\Component\Finder\Finder;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class BackgroundImageExtension extends AbstractExtension
{
public function __construct(
private string $cacheDir
private string $backgroundImagesDirectory,
private CacheInterface $cacheAdapter,
) {
}

Expand All @@ -24,8 +27,20 @@ public function getFunctions(): array

public function getBackgroundImages(): array
{
$filePath = $this->cacheDir.'/'.BackgroundImageCacheWarmer::getCacheFileName();
return $this->cacheAdapter->get('background_images', function (ItemInterface $item): array {
$backgroundImages = $this->findBackgroundImagesToCache();
$item->set($backgroundImages);

return include $filePath;
return $backgroundImages;
});
}

private function findBackgroundImagesToCache(): array
{
$imagesIterator = Finder::create()->in($this->backgroundImagesDirectory)->files()->getIterator();

$images = array_map(static fn (\SplFileInfo $imageFile) => $imageFile->getFilename(), iterator_to_array($imagesIterator));

return array_values($images);
}
}

0 comments on commit 9e01769

Please sign in to comment.