Add tags for flushing cache

This commit is contained in:
Philipp Lang 2021-09-06 15:35:45 +02:00
parent 0f478507d3
commit 70f66aaacc
4 changed files with 18 additions and 6 deletions

View File

@ -72,6 +72,7 @@ class Plugin extends PluginBase
}); });
Event::listen('media.file.delete', function($widget, $filePath) { Event::listen('media.file.delete', function($widget, $filePath) {
app(FileObserver::class)->delete($filePath); app(FileObserver::class)->delete($filePath);
app(CacheManager::class)->delete($filePath);
}); });
Event::listen('media.file.move', function($widget, $old, $new) { Event::listen('media.file.move', function($widget, $old, $new) {

View File

@ -18,7 +18,19 @@ class CacheManager
public function get(string $path, string $size): string 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 private function cacheKey(string $path, string $size): string

View File

@ -20,7 +20,7 @@ class DeleteTest extends TestCase
public function testItDeletesAllVersionsIfOriginalImageDeleted() 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('folders', ['pages']);
Setting::set('sizes', []); Setting::set('sizes', []);
Setting::set('breakpoints', []); Setting::set('breakpoints', []);

View File

@ -96,7 +96,7 @@ class ImageTagTest extends TestCase
UploadedFile::fake()->image('test.jpg', 640, 149)->storeAs('uploads/public/c/pages', 'test-640x149.jpg', 'local'); 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}}'); $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() public function testPickOriginalImageWhenSvgIsGiven()
@ -111,7 +111,7 @@ class ImageTagTest extends TestCase
$this->assertFalse(str_contains($output, 'srcset')); $this->assertFalse(str_contains($output, 'srcset'));
$this->assertFalse(str_contains($output, 'sizes')); $this->assertFalse(str_contains($output, 'sizes'));
$this->assertTrue(str_contains($output, 'src="'.$this->media->findFiles('/pages/test.svg')[0]->publicUrl.'"')); $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() public function testNormalizeFilePathForCache()
@ -122,8 +122,7 @@ class ImageTagTest extends TestCase
$this->media->put('/pages/test.svg', file_get_contents(__DIR__.'/stub/close.svg')); $this->media->put('/pages/test.svg', file_get_contents(__DIR__.'/stub/close.svg'));
$output = app(Twig::class)->parse('{{"/pages/test.svg" | resize}}'); $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::get('resize.original.pages/test.svg'));
} }
} }