Skip to content

Commit

Permalink
fix: candidates cache
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio committed Nov 24, 2024
1 parent 8cae91e commit 5ddd2c7
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions app/Livewire/Pages/ElectionResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

namespace App\Livewire\Pages;

use App\Enums\Time;
use App\Models\Candidate;
use App\Models\Party;
use App\Models\Vote;
use App\Repositories\RecordsRepository;
use App\Repositories\VotesRepository;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Number;
use Illuminate\View\View;
use Livewire\Attributes\Computed;
Expand All @@ -32,25 +34,29 @@ public function render(): View
#[Computed()]
public function parties(): Collection
{
return Party::query()
->whereBelongsTo($this->election)
->whereHas('votes', function (Builder $query) {
$query->whereBelongsTo($this->election);
})
->with('media')
->get();
return Cache::remember("parties-with-votes:{$this->election->id}", Time::DAY_IN_SECONDS, function () {
return Party::query()
->whereBelongsTo($this->election)
->whereHas('votes', function (Builder $query) {
$query->whereBelongsTo($this->election);
})
->with('media')
->get();
});
}

#[Computed()]
public function candidates(): Collection
{
return Candidate::query()
->whereBelongsTo($this->election)
->whereHas('votes', function (Builder $query) {
$query->whereBelongsTo($this->election);
})
->with('media')
->get();
return Cache::remember("candidates-with-votes:{$this->election->id}", Time::DAY_IN_SECONDS, function () {
return Candidate::query()
->whereBelongsTo($this->election)
->whereHas('votes', function (Builder $query) {
$query->whereBelongsTo($this->election);
})
->with('media')
->get();
});
}

#[Computed]
Expand Down

0 comments on commit 5ddd2c7

Please sign in to comment.