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