From 0bad9391fc44d48bef1c050fe62efd75afb843bb Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 4 Feb 2026 08:39:57 +0000 Subject: [PATCH] Cache allow_register setting query to reduce DB load - Wrapped the query in `SettingService::isRegisterAllowed` with `Cache::remember`. - Updated `tests/Feature/Auth/RegistrationTest.php` to clear cache after setting updates and mock Captcha HTTP requests. Co-authored-by: yilanboy <27554321+yilanboy@users.noreply.github.com> --- app/Services/SettingService.php | 11 +++++++---- tests/Feature/Auth/RegistrationTest.php | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/app/Services/SettingService.php b/app/Services/SettingService.php index e6a0e871..eaf0f5c5 100644 --- a/app/Services/SettingService.php +++ b/app/Services/SettingService.php @@ -5,15 +5,18 @@ namespace App\Services; use App\Models\Setting; +use Illuminate\Support\Facades\Cache; class SettingService { public static function isRegisterAllowed(): bool { - $isRegisterAllow = Setting::query() - ->where('key', 'allow_register') - ->first() - ->value; + $isRegisterAllow = Cache::remember('setting:allow_register', 3600, function () { + return Setting::query() + ->where('key', 'allow_register') + ->first() + ->value; + }); return filter_var($isRegisterAllow, FILTER_VALIDATE_BOOLEAN); } diff --git a/tests/Feature/Auth/RegistrationTest.php b/tests/Feature/Auth/RegistrationTest.php index fa980a8d..7e394d89 100644 --- a/tests/Feature/Auth/RegistrationTest.php +++ b/tests/Feature/Auth/RegistrationTest.php @@ -2,14 +2,24 @@ use App\Models\Setting; use App\Models\User; +use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Http; use function Pest\Laravel\get; beforeEach(function () { + Http::fake([ + 'https://challenges.cloudflare.com/turnstile/v0/siteverify' => Http::response([ + 'success' => true, + ]), + ]); + Setting::query() ->where('key', 'allow_register') ->firstOrFail() ->update(['value' => true]); + + Cache::forget('setting:allow_register'); }); test('registration screen can be rendered', function () { @@ -214,6 +224,8 @@ ->firstOrFail() ->update(['value' => false]); + Cache::forget('setting:allow_register'); + get(route('register'))->assertStatus(503); }); @@ -223,5 +235,7 @@ ->firstOrFail() ->update(['value' => false]); + Cache::forget('setting:allow_register'); + Livewire::test('layouts.header')->assertDontSeeText('註冊'); });