From 45b597bbab736bb0a4d06e0bd700b5cf606a3100 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 8 Sep 2023 18:33:26 +0200 Subject: [PATCH] feat: cache team settings --- app/Http/Livewire/Boarding/Index.php | 4 +++- app/Http/Livewire/SwitchTeam.php | 2 +- app/Http/Middleware/IsBoardingFlow.php | 2 +- app/Models/User.php | 5 ++++- bootstrap/helpers/shared.php | 10 ++++++++-- 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/app/Http/Livewire/Boarding/Index.php b/app/Http/Livewire/Boarding/Index.php index 35f7fd218..c68cd8a50 100644 --- a/app/Http/Livewire/Boarding/Index.php +++ b/app/Http/Livewire/Boarding/Index.php @@ -6,6 +6,7 @@ use App\Actions\Server\InstallDocker; use App\Models\PrivateKey; use App\Models\Project; use App\Models\Server; +use App\Models\Team; use Illuminate\Support\Collection; use Livewire\Component; @@ -70,9 +71,10 @@ uZx9iFkCELtxrh31QJ68AAAAEXNhaWxANzZmZjY2ZDJlMmRkAQIDBA== } public function skipBoarding() { - currentTeam()->update([ + Team::find(currentTeam()->id)->update([ 'show_boarding' => false ]); + ray(currentTeam()); refreshSession(); return redirect()->route('dashboard'); } diff --git a/app/Http/Livewire/SwitchTeam.php b/app/Http/Livewire/SwitchTeam.php index 087a7ad84..9a9b4c795 100644 --- a/app/Http/Livewire/SwitchTeam.php +++ b/app/Http/Livewire/SwitchTeam.php @@ -23,7 +23,7 @@ class SwitchTeam extends Component if (!$team_to_switch_to) { return; } - session(['currentTeam' => $team_to_switch_to]); + refreshSession($team_to_switch_to); return redirect(request()->header('Referer')); } } diff --git a/app/Http/Middleware/IsBoardingFlow.php b/app/Http/Middleware/IsBoardingFlow.php index 5858fe191..e0542a57a 100644 --- a/app/Http/Middleware/IsBoardingFlow.php +++ b/app/Http/Middleware/IsBoardingFlow.php @@ -15,7 +15,7 @@ class IsBoardingFlow */ public function handle(Request $request, Closure $next): Response { - // ray()->showQueries()->color('orange'); + ray()->showQueries()->color('orange'); if (showBoarding() && !in_array($request->path(), allowedPathsForBoardingAccounts())) { return redirect('boarding'); } diff --git a/app/Models/User.php b/app/Models/User.php index 9050de214..1671db496 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -4,6 +4,7 @@ namespace App\Models; use App\Notifications\Channels\SendsEmail; use App\Notifications\TransactionalEmails\ResetPassword as TransactionalEmailsResetPassword; +use Cache; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; @@ -94,7 +95,9 @@ class User extends Authenticatable implements SendsEmail public function currentTeam() { - return Team::find(session('currentTeam')->id); + return Cache::remember('team:' . auth()->user()->id, 3600, function() { + return Team::find(session('currentTeam')->id); + }); } public function otherTeams() diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.php index 161675301..7a8c5429b 100644 --- a/bootstrap/helpers/shared.php +++ b/bootstrap/helpers/shared.php @@ -57,9 +57,15 @@ function showBoarding(): bool { return currentTeam()->show_boarding ?? false; } -function refreshSession(): void +function refreshSession(?Team $team = null): void { - $team = Team::find(currentTeam()->id); + if (!$team) { + $team = Team::find(currentTeam()->id); + } + Cache::forget('team:' . auth()->user()->id); + Cache::remember('team:' . auth()->user()->id, 3600, function() use ($team) { + return $team; + }); session(['currentTeam' => $team]); } function general_error_handler(Throwable | null $err = null, $that = null, $isJson = false, $customErrorMessage = null): mixed