Bulk update context to prevent cache thrashing

This commit is contained in:
David Bomba 2025-01-24 16:09:55 +11:00
parent 1f461ac79a
commit 1a5737478f
3 changed files with 43 additions and 18 deletions

View File

@ -131,11 +131,15 @@ class InvoicePay extends Component
#[On('payment-method-selected')] #[On('payment-method-selected')]
public function paymentMethodSelected($company_gateway_id, $gateway_type_id, $amount) public function paymentMethodSelected($company_gateway_id, $gateway_type_id, $amount)
{ {
$this->setContext('company_gateway_id', $company_gateway_id);
$this->setContext('gateway_type_id', $gateway_type_id); $this->bulkSetContext([
$this->setContext('amount', $amount); 'company_gateway_id' => $company_gateway_id,
$this->setContext('pre_payment', false); 'gateway_type_id' => $gateway_type_id,
$this->setContext('is_recurring', false); 'amount' => $amount,
'pre_payment' => false,
'is_recurring' => false,
]);
$this->payment_method_accepted = true; $this->payment_method_accepted = true;
@ -236,10 +240,14 @@ class InvoicePay extends Component
$client = $invite->contact->client; $client = $invite->contact->client;
$settings = $client->getMergedSettings(); $settings = $client->getMergedSettings();
$this->setContext('contact', $invite->contact); // $this->context['contact'] = $invite->contact;
$this->setContext('settings', $settings); // $this->context['settings'] = $settings; $this->bulkSetContext([
$this->setContext('db', $this->db); // $this->context['db'] = $this->db; 'contact' => $invite->contact,
$this->setContext('invitation_id', $this->invitation_id); 'settings' => $settings,
'db' => $this->db,
'invitation_id' => $this->invitation_id,
]);
$invoices = Invoice::withTrashed() $invoices = Invoice::withTrashed()
->whereIn('id', $this->transformKeys($this->invoices)) ->whereIn('id', $this->transformKeys($this->invoices))
@ -262,11 +270,6 @@ class InvoicePay extends Component
$this->under_over_payment = $settings->client_portal_allow_over_payment || $settings->client_portal_allow_under_payment; $this->under_over_payment = $settings->client_portal_allow_over_payment || $settings->client_portal_allow_under_payment;
$this->required_fields = false; $this->required_fields = false;
$this->setContext('variables', $this->variables); // $this->context['variables'] = $this->variables;
$this->setContext('invoices', $invoices); // $this->context['invoices'] = $invoices;
$this->setContext('settings', $settings); // $this->context['settings'] = $settings;
// $this->setContext('invitation', $invite->withoutRelations()); // $this->context['invitation'] = $invite;
$payable_invoices = $invoices->map(function ($i) { $payable_invoices = $invoices->map(function ($i) {
/** @var \App\Models\Invoice $i */ /** @var \App\Models\Invoice $i */
return [ return [
@ -281,8 +284,14 @@ class InvoicePay extends Component
]; ];
})->toArray(); })->toArray();
$this->setContext('amount', array_sum(array_column($payable_invoices, 'amount'))); $this->bulkSetContext([
$this->setContext('payable_invoices', $payable_invoices); 'variables' => $this->variables,
'invoices' => $invoices,
'settings' => $settings,
'amount' => array_sum(array_column($payable_invoices, 'amount')),
'payable_invoices' => $payable_invoices,
]);
} }
public function render(): \Illuminate\Contracts\View\Factory|\Illuminate\View\View public function render(): \Illuminate\Contracts\View\Factory|\Illuminate\View\View

View File

@ -63,8 +63,10 @@ class ProcessPayment extends Component
$gateway_fee = data_get($responder_data, 'payload.total.fee_total', false); $gateway_fee = data_get($responder_data, 'payload.total.fee_total', false);
$amount = data_get($responder_data, 'payload.total.amount_with_fee', 0); $amount = data_get($responder_data, 'payload.total.amount_with_fee', 0);
$this->setContext('amount', $amount); $this->bulkSetContext([
$this->setContext('gateway_fee', $gateway_fee); 'amount' => $amount,
'gateway_fee' => $gateway_fee,
]);
$this->dispatch('payment-view-rendered'); $this->dispatch('payment-view-rendered');
} }

View File

@ -52,6 +52,20 @@ trait WithSecureContext
return $clone; return $clone;
} }
public function bulkSetContext(array $data): array
{
$clone = $this->getContext();
$clone = array_merge($clone, $data);
\Illuminate\Support\Facades\Cache::put(session()->getId(), $clone, now()->addHour());
$this->dispatch(self::CONTEXT_UPDATE);
return $clone;
}
public function resetContext(): void public function resetContext(): void
{ {
\Illuminate\Support\Facades\Cache::forget(session()->getId()); \Illuminate\Support\Facades\Cache::forget(session()->getId());