Skip to content

Commit 163c485

Browse files
committed
CHORE - Add cache to social share image response
1 parent 1f1b824 commit 163c485

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

app/Jobs/GenerateSocialShareImage.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Models\Article;
66
use Illuminate\Http\Response;
7+
use Illuminate\Support\Facades\Cache;
78
use Intervention\Image\Drivers\Gd\Driver;
89
use Intervention\Image\ImageManager;
910

@@ -30,14 +31,21 @@ public function handle(): Response
3031
$image = new ImageManager(new Driver);
3132
$text = wordwrap($this->article->title(), self::CHARACTERS_PER_LINE);
3233

33-
return response(
34-
$image->read(resource_path('images/' . self::TEMPLATE))
35-
->text($text, self::TEXT_X_POSITION, self::TEXT_Y_POSITION, function ($font) {
36-
$font->file(resource_path('fonts/' . self::FONT));
37-
$font->size(self::FONT_SIZE);
38-
$font->color(self::TEXT_COLOUR);
39-
})
40-
->toPng()
41-
)->header('Content-Type', 'image/png');
34+
return Cache::remember(
35+
'articleSocialImage-' . $this->article->id,
36+
now()->addDay(),
37+
function () use ($image, $text) {
38+
return response(
39+
$image->read(resource_path('images/' . self::TEMPLATE))
40+
->text($text, self::TEXT_X_POSITION, self::TEXT_Y_POSITION, function ($font) {
41+
$font->file(resource_path('fonts/' . self::FONT));
42+
$font->size(self::FONT_SIZE);
43+
$font->color(self::TEXT_COLOUR);
44+
})
45+
->toPng()
46+
)->header('Content-Type', 'image/png')
47+
->header('Cache-Control', 'max-age=86400, public');
48+
}
49+
);
4250
}
4351
}

0 commit comments

Comments
 (0)