From 425a6aa3bee2a1708bd7d1f4f348b7b8cf6f1fcd Mon Sep 17 00:00:00 2001 From: philipp lang Date: Mon, 27 Dec 2021 02:18:56 +0100 Subject: [PATCH] Fixed: Clear cache after resizing image --- classes/CacheManager.php | 6 +++--- classes/ResizeJob.php | 6 +++++- tests/ResizerTest.php | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/classes/CacheManager.php b/classes/CacheManager.php index 55727e7..1743f0b 100644 --- a/classes/CacheManager.php +++ b/classes/CacheManager.php @@ -18,7 +18,7 @@ class CacheManager public function get(MediaPath $path, string $size, ?string $sizes, array $options = []): string { - return Cache::tags($this->pathTag($path, $size))->rememberForever( + return Cache::tags($this->pathTag($path))->rememberForever( $this->cacheKey($path, $size), fn () => $this->tagGenerator->generate($path, $size, $sizes, $options) ); @@ -26,7 +26,7 @@ class CacheManager public function delete(MediaPath $path): void { - Cache::tags([$this->singlePathTag($path)])->flush(); + Cache::tags($this->pathTag($path))->flush(); } public function flush(): void @@ -39,7 +39,7 @@ class CacheManager return "resizer.{$size}.{$path->normal()}"; } - private function pathTag(MediaPath $path, string $size): array + private function pathTag(MediaPath $path): array { return [$this->tagAll, $this->singlePathTag($path)]; } diff --git a/classes/ResizeJob.php b/classes/ResizeJob.php index b3d37e7..a37ba97 100644 --- a/classes/ResizeJob.php +++ b/classes/ResizeJob.php @@ -2,6 +2,7 @@ namespace Aweos\Resizer\Classes; +use Aweos\Resizer\Classes\CacheManager; use Aweos\Resizer\Lib\MediaPath; use Log; use Throwable; @@ -12,7 +13,10 @@ class ResizeJob public function fire($job, $params) { list($file) = $params; - app(ImageResizer::class)->generate(new MediaPath($file), $params['update'] ?? false); + $media = new MediaPath($file); + app(ImageResizer::class)->generate($media, $params['update'] ?? false); + app(CacheManager::class)->delete($media); + $job->delete(); } diff --git a/tests/ResizerTest.php b/tests/ResizerTest.php index 62e83a7..1a6b64c 100644 --- a/tests/ResizerTest.php +++ b/tests/ResizerTest.php @@ -7,6 +7,7 @@ use Aweos\Resizer\Models\Setting; use Aweos\Resizer\Tests\TestCase; use Event; use Illuminate\Http\UploadedFile; +use Illuminate\Support\Facades\Cache; use MediaLibrary; use Storage; @@ -33,6 +34,21 @@ class ResizerTest extends TestCase $this->assertFileCount(0, ''); } + public function testDeleteOldCacheWhenUploadingFile(): void + { + Setting::set('folders', [['folder' => '/pages']]); + Setting::set('sizes', []); + Setting::set('breakpoints', []); + $file = UploadedFile::fake()->image('test.jpg', 500, 600); + $this->media->put('/pages/test.jpg', $file); + Cache::tags(['resizer', 'resizer.pages/test.jpg'])->put('resizer.original.pages/test.jpg', 'AAA'); + + Event::fire('media.file.upload', [null, '/pages/test.jpg', null]); + + $this->assertFileCount(0, ''); + $this->assertFalse(Cache::tags(['resizer', 'resizer.pages/test.jpg'])->has('resizer.original.pages/test.jpg')); + } + public function testThrowExceptionWhenOriginalFileNotFound(): void { $this->expectException(ResizerException::class);