From 0f478507d33511185f4657a624f3cf80e0ba24c6 Mon Sep 17 00:00:00 2001 From: Philipp Lang Date: Mon, 6 Sep 2021 14:37:35 +0200 Subject: [PATCH] Ad Cachemanager --- Plugin.php | 6 ++---- classes/CacheManager.php | 31 +++++++++++++++++++++++++++++++ tests/DeleteTest.php | 3 +++ 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 classes/CacheManager.php 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()