Add cache

This commit is contained in:
Philipp Lang 2021-09-06 14:11:10 +02:00
parent ed17bd1945
commit 3f91586b62
2 changed files with 20 additions and 13 deletions

View File

@ -100,7 +100,8 @@ class Plugin extends PluginBase
return [ return [
'filters' => [ 'filters' => [
'resize' => function($media, $size = 'original') { 'resize' => function($media, $size = 'original') {
return Cache::rememberForever("resize.{$size}.{$media}", fn () => app(TagGenerator::class)->generate($media, $size)); $normalPath = app(FileObserver::class)->normalizePath($media);
return Cache::rememberForever("resize.{$size}.{$normalPath}", fn () => app(TagGenerator::class)->generate($media, $size));
}, },
] ]
]; ];

View File

@ -105,19 +105,25 @@ class ImageTagTest extends TestCase
Setting::set('sizes', [['name' => 'size', 'aspect_ratio' => '1200x280']]); Setting::set('sizes', [['name' => 'size', 'aspect_ratio' => '1200x280']]);
Setting::set('breakpoints', [640]); Setting::set('breakpoints', [640]);
$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}}');
$this->assertFalse(str_contains( $this->assertFalse(str_contains($output, 'srcset'));
app(Twig::class)->parse('{{"pages/test.svg" | resize}}'), $this->assertFalse(str_contains($output, 'sizes'));
'srcset', $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->assertFalse(str_contains( }
app(Twig::class)->parse('{{"pages/test.svg" | resize}}'),
'sizes', public function testNormalizeFilePathForCache()
)); {
$this->assertTrue(str_contains( Setting::set('folders', ['pages']);
app(Twig::class)->parse('{{"pages/test.svg" | resize}}'), Setting::set('sizes', [['name' => 'size', 'aspect_ratio' => '1200x280']]);
'src="'.$this->media->findFiles('/pages/test.svg')[0]->publicUrl.'"', Setting::set('breakpoints', [640]);
)); $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'));
} }
} }