diff --git a/Plugin.php b/Plugin.php index 435a43c..c9ba7bd 100644 --- a/Plugin.php +++ b/Plugin.php @@ -77,9 +77,11 @@ class Plugin extends PluginBase Event::listen('media.file.move', function($widget, $old, $new) { app(FileObserver::class)->rename($old, $new); + app(CacheManager::class)->delete($old); }); Event::listen('media.file.rename', function($widget, $old, $new) { app(FileObserver::class)->rename($old, $new); + app(CacheManager::class)->delete($old); }); } diff --git a/classes/CacheManager.php b/classes/CacheManager.php index d36013f..665166c 100644 --- a/classes/CacheManager.php +++ b/classes/CacheManager.php @@ -25,19 +25,19 @@ class CacheManager public function delete(string $path): void { $path = $this->fileObserver->normalizePath($path); - Cache::tags($path)->flush(); + Cache::tags("resizer.$path")->flush(); } - private function getTag(string $path): string + private function getTag(string $path): array { - return $this->fileObserver->normalizePath($path); + return ['resizer', "resizer.{$this->fileObserver->normalizePath($path)}"]; } private function cacheKey(string $path, string $size): string { $normalPath = $this->fileObserver->normalizePath($path); - return "resize.{$size}.{$normalPath}"; + return "resizer.{$size}.{$normalPath}"; } } diff --git a/tests/ImageTagTest.php b/tests/ImageTagTest.php index 69c6d4a..be130a4 100644 --- a/tests/ImageTagTest.php +++ b/tests/ImageTagTest.php @@ -96,7 +96,7 @@ class ImageTagTest extends TestCase UploadedFile::fake()->image('test.jpg', 640, 149)->storeAs('uploads/public/c/pages', 'test-640x149.jpg', 'local'); $output = app(Twig::class)->parse('{{"pages/test.jpg" | resize}}'); - $this->assertEquals($output, Cache::tags('pages/test.jpg')->get('resize.original.pages/test.jpg')); + $this->assertEquals($output, Cache::tags(['resizer', 'resizer.pages/test.jpg'])->get('resizer.original.pages/test.jpg')); } public function testPickOriginalImageWhenSvgIsGiven() @@ -111,7 +111,7 @@ class ImageTagTest extends TestCase $this->assertFalse(str_contains($output, 'srcset')); $this->assertFalse(str_contains($output, 'sizes')); $this->assertTrue(str_contains($output, 'src="'.$this->media->findFiles('/pages/test.svg')[0]->publicUrl.'"')); - $this->assertEquals($output, Cache::tags('pages/test.svg')->get('resize.original.pages/test.svg')); + $this->assertEquals($output, Cache::tags(['resizer', 'resizer.pages/test.svg'])->get('resizer.original.pages/test.svg')); } public function testNormalizeFilePathForCache() @@ -122,7 +122,7 @@ class ImageTagTest extends TestCase $this->media->put('/pages/test.svg', file_get_contents(__DIR__.'/stub/close.svg')); $output = app(Twig::class)->parse('{{"/pages/test.svg" | resize}}'); - $this->assertEquals($output, Cache::tags('pages/test.svg')->get('resize.original.pages/test.svg')); + $this->assertEquals($output, Cache::tags(['resizer', 'resizer.pages/test.svg'])->get('resizer.original.pages/test.svg')); } } diff --git a/tests/MoveTest.php b/tests/MoveTest.php index f2b3005..aa827e7 100644 --- a/tests/MoveTest.php +++ b/tests/MoveTest.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 MoveTest extends TestCase public function testItMovesAllVersionsOfAFile() { + Cache::tags(['resizer', 'resizer.pages/alt/test.jpg'])->set('resizer.original.pages/alt/test.jpg', 'aa'); Setting::set('folders', ['pages']); Setting::set('sizes', []); Setting::set('breakpoints', []); @@ -31,6 +33,7 @@ class MoveTest extends TestCase $this->assertFileCount(0, 'pages/alt'); $this->assertHasFile('pages/neu/test.jpg'); $this->assertHasFile('pages/neu/test-200x300.jpg'); + $this->assertNull(Cache::tags(['resizer', 'resizer.pages/alt/test.jpg'])->get('resizer.original.pages/alt/test.jpg')); } public function testItMovesFilesOnRename() @@ -51,6 +54,7 @@ class MoveTest extends TestCase public function testItDoesntMoveOtherFilesInTheSameDirectory() { + Cache::tags(['resizer', 'resizer.pages/test.jpg'])->set('resizer.original.pages/test.jpg', 'aa'); Setting::set('folders', ['pages']); Setting::set('sizes', []); Setting::set('breakpoints', []); @@ -64,6 +68,7 @@ class MoveTest extends TestCase $this->assertFileCount(3, 'pages'); $this->assertHasFile('pages/testneu.jpg'); $this->assertHasFile('pages/testneu-200x300.jpg'); + $this->assertNull(Cache::tags(['resizer', 'resizer.pages/test.jpg'])->get('resizer.original.pages/test.jpg')); } }