diff --git a/Plugin.php b/Plugin.php index f3f6eb2..c9e0524 100644 --- a/Plugin.php +++ b/Plugin.php @@ -1,5 +1,6 @@ [ - 'resize' => function($media, $size = 'original') { - $normalPath = app(FileObserver::class)->normalizePath($media); - return Cache::rememberForever("resize.{$size}.{$normalPath}", fn () => app(TagGenerator::class)->generate($media, $size)); - }, + 'resize' => fn ($media, $size = 'original') => app(CacheManager::class)->get($media, $size) ] ]; } diff --git a/classes/CacheManager.php b/classes/CacheManager.php new file mode 100644 index 0000000..071aa34 --- /dev/null +++ b/classes/CacheManager.php @@ -0,0 +1,31 @@ +tag = $tag; + $this->fileObserver = $fileObserver; + } + + public function get(string $path, string $size): string + { + return Cache::rememberForever($this->cacheKey($path, $size), fn () => $this->tag->generate($path, $size)); + } + + private function cacheKey(string $path, string $size): string + { + $normalPath = $this->fileObserver->normalizePath($path); + + return "resize.{$size}.{$normalPath}"; + } + +} diff --git a/tests/DeleteTest.php b/tests/DeleteTest.php index 7056064..9186e05 100644 --- a/tests/DeleteTest.php +++ b/tests/DeleteTest.php @@ -4,6 +4,7 @@ namespace Aweos\Resizer\Tests\MediaTest; use Aweos\Resizer\Models\Setting; use Aweos\Resizer\Tests\TestCase; +use Cache; use Event; use Illuminate\Http\UploadedFile; use Storage; @@ -19,6 +20,7 @@ class DeleteTest extends TestCase public function testItDeletesAllVersionsIfOriginalImageDeleted() { + Cache::set('resizer.original.pages/test.jpg', '::cacheContent::'); Setting::set('folders', ['pages']); Setting::set('sizes', []); Setting::set('breakpoints', []); @@ -28,6 +30,7 @@ class DeleteTest extends TestCase $this->assertFileCount(0, 'pages'); Storage::assertMissing('uploads/public/c/pages'); + $this->assertNull(Cache::get('resizer.original.pages/test.jpg')); } public function testItPreservesDirectoryWhenThereAreOtherFiles()