From cf745ec73a4ea8376204ba3a6c2358d806a0d282 Mon Sep 17 00:00:00 2001 From: Wisp X Date: Wed, 19 Jan 2022 13:33:10 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E5=9B=BE=E7=89=87=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Enums/GroupConfigKey.php | 3 +++ app/Http/Controllers/User/ImageController.php | 27 ++++++++++++++----- database/seeders/DatabaseSeeder.php | 1 + 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/app/Enums/GroupConfigKey.php b/app/Enums/GroupConfigKey.php index 75fe939e..6d10cac2 100644 --- a/app/Enums/GroupConfigKey.php +++ b/app/Enums/GroupConfigKey.php @@ -45,4 +45,7 @@ final class GroupConfigKey /** @var string 文件命名规则 */ const FileNamingRule = 'file_naming_rule'; + + /** @var string 图片缓存时间 */ + const CacheTtl = 'cache_ttl'; } diff --git a/app/Http/Controllers/User/ImageController.php b/app/Http/Controllers/User/ImageController.php index bfe23f5c..6e1471cc 100644 --- a/app/Http/Controllers/User/ImageController.php +++ b/app/Http/Controllers/User/ImageController.php @@ -104,16 +104,29 @@ class ImageController extends Controller abort(404); } try { - $contents = $image->filesystem()->read($image->pathname); + $cacheKey = "image_{$image->key}"; + + if (Cache::has($cacheKey)) { + $contents = Cache::get($cacheKey); + } else { + $contents = $image->filesystem()->read($image->pathname); + // 是否启用了水印功能,跳过gif图片 + if ($image->group->configs->get(GroupConfigKey::IsEnableWatermark) && $image->mimetype !== 'image/gif') { + $configs = $image->group->configs->get(GroupConfigKey::WatermarkConfigs); + $contents = (string)$service->stickWatermark($contents, collect($configs))->encode(); + } + $cacheTtl = (int)$image->group->configs->get(GroupConfigKey::CacheTtl, 0); + // 是否启用了缓存 + if ($cacheTtl) { + Cache::remember($cacheKey, $cacheTtl, fn () => $contents); + } else { + if (Cache::has($cacheKey)) Cache::forget($cacheKey); + } + } } catch (FilesystemException $e) { abort(404); } - // 是否启用了水印功能,跳过gif图片 - if ($image->group->configs->get(GroupConfigKey::IsEnableWatermark) && $image->mimetype !== 'image/gif') { - // TODO 缓存水印文件 - $configs = $image->group->configs->get(GroupConfigKey::WatermarkConfigs); - $contents = (string)$service->stickWatermark($contents, collect($configs))->encode(); - } + return \response()->stream(function () use ($contents) { echo $contents; }, headers: ['Content-type' => $image->mimetype]); diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 9ebf3b24..e0e13567 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -83,6 +83,7 @@ class DatabaseSeeder extends Seeder GroupConfigKey::AcceptedFileSuffixes => ['jpeg', 'png', 'gif', 'tif', 'bmp', 'ico', 'psd', 'webp'], GroupConfigKey::PathNamingRule => '{Y}/{m}/{d}', GroupConfigKey::FileNamingRule => '{uniqid}', + GroupConfigKey::CacheTtl => 2626560, ])->toJson(), ])->transform(function ($value, $key) use ($date) { return ['name' => $key, 'value' => $value, 'updated_at' => $date, 'created_at' => $date];