diff --git a/Plugin.php b/Plugin.php index 743e082..07031da 100644 --- a/Plugin.php +++ b/Plugin.php @@ -100,7 +100,7 @@ class Plugin extends PluginBase return [ 'filters' => [ 'resize' => function($media, $size = 'original') { - return app(TagGenerator::class)->generate($media, $size); + return Cache::rememberForever("resize.{$size}.{$media}", fn () => app(TagGenerator::class)->generate($media, $size)); }, ] ]; diff --git a/tests/ImageTagTest.php b/tests/ImageTagTest.php index d88149b..c2077c8 100644 --- a/tests/ImageTagTest.php +++ b/tests/ImageTagTest.php @@ -5,8 +5,10 @@ namespace Aweos\Resizer\Tests\MediaTest; use Aweos\Resizer\Classes\TagGenerator; use Aweos\Resizer\Models\Setting; use Aweos\Resizer\Tests\TestCase; +use Cache; use Event; use Illuminate\Http\UploadedFile; +use October\Rain\Parse\Twig; use Storage; class ImageTagTest extends TestCase @@ -85,6 +87,18 @@ class ImageTagTest extends TestCase )); } + public function testGenerateCache() + { + Setting::set('folders', ['pages']); + Setting::set('sizes', [['name' => 'size', 'aspect_ratio' => '1200x280']]); + Setting::set('breakpoints', [640]); + $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 1920, 899)->get()); + 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')); + } + public function testPickOriginalImageWhenSvgIsGiven() { Setting::set('folders', ['pages']);