From 70f66aaacc076815a2a191b419ee5181e023b8b1 Mon Sep 17 00:00:00 2001 From: Philipp Lang Date: Mon, 6 Sep 2021 15:35:45 +0200 Subject: [PATCH] Add tags for flushing cache --- Plugin.php | 1 + classes/CacheManager.php | 14 +++++++++++++- tests/DeleteTest.php | 2 +- tests/ImageTagTest.php | 7 +++---- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Plugin.php b/Plugin.php index c9e0524..435a43c 100644 --- a/Plugin.php +++ b/Plugin.php @@ -72,6 +72,7 @@ class Plugin extends PluginBase }); Event::listen('media.file.delete', function($widget, $filePath) { app(FileObserver::class)->delete($filePath); + app(CacheManager::class)->delete($filePath); }); Event::listen('media.file.move', function($widget, $old, $new) { diff --git a/classes/CacheManager.php b/classes/CacheManager.php index 071aa34..d36013f 100644 --- a/classes/CacheManager.php +++ b/classes/CacheManager.php @@ -18,7 +18,19 @@ class CacheManager public function get(string $path, string $size): string { - return Cache::rememberForever($this->cacheKey($path, $size), fn () => $this->tag->generate($path, $size)); + return Cache::tags($this->getTag($path)) + ->rememberForever($this->cacheKey($path, $size), fn () => $this->tag->generate($path, $size)); + } + + public function delete(string $path): void + { + $path = $this->fileObserver->normalizePath($path); + Cache::tags($path)->flush(); + } + + private function getTag(string $path): string + { + return $this->fileObserver->normalizePath($path); } private function cacheKey(string $path, string $size): string diff --git a/tests/DeleteTest.php b/tests/DeleteTest.php index 9186e05..8955ed4 100644 --- a/tests/DeleteTest.php +++ b/tests/DeleteTest.php @@ -20,7 +20,7 @@ class DeleteTest extends TestCase public function testItDeletesAllVersionsIfOriginalImageDeleted() { - Cache::set('resizer.original.pages/test.jpg', '::cacheContent::'); + Cache::tags('pages/test.jpg')->set('resizer.original.pages/test.jpg', '::cacheContent::'); Setting::set('folders', ['pages']); Setting::set('sizes', []); Setting::set('breakpoints', []); diff --git a/tests/ImageTagTest.php b/tests/ImageTagTest.php index b5f45f8..69c6d4a 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::get('resize.original.pages/test.jpg')); + $this->assertEquals($output, Cache::tags('pages/test.jpg')->get('resize.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::get('resize.original.pages/test.svg')); + $this->assertEquals($output, Cache::tags('pages/test.svg')->get('resize.original.pages/test.svg')); } public function testNormalizeFilePathForCache() @@ -122,8 +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::get('resize.original.pages/test.svg')); + $this->assertEquals($output, Cache::tags('pages/test.svg')->get('resize.original.pages/test.svg')); } }