Implement better caching of results page.

This commit is contained in:
Matt Young 2024-11-08 22:02:43 -06:00
parent 2bd5d2fff8
commit 01f520b587
4 changed files with 56 additions and 34 deletions

View File

@ -27,5 +27,6 @@ class PublishSeats
} }
$audition->addFlag('seats_published'); $audition->addFlag('seats_published');
Cache::forget('resultsSeatList'); Cache::forget('resultsSeatList');
Cache::forget('publicResultsPage');
} }
} }

View File

@ -16,6 +16,7 @@ class UnpublishSeats
{ {
$audition->removeFlag('seats_published'); $audition->removeFlag('seats_published');
Cache::forget('resultsSeatList'); Cache::forget('resultsSeatList');
Cache::forget('publicResultsPage');
Seat::where('audition_id', $audition->id)->delete(); Seat::where('audition_id', $audition->id)->delete();
} }
} }

View File

@ -10,6 +10,7 @@ use App\Models\Seat;
use App\Services\AuditionService; use App\Services\AuditionService;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\View;
use function auditionSetting; use function auditionSetting;
@ -27,6 +28,12 @@ class ResultsPage extends Controller
*/ */
public function __invoke(Request $request) public function __invoke(Request $request)
{ {
$cacheKey = 'publicResultsPage';
if (Cache::has($cacheKey)) {
return response(Cache::get($cacheKey));
}
$publishedAdvancementAuditions = []; $publishedAdvancementAuditions = [];
$resultsAdvancementList = []; $resultsAdvancementList = [];
$publishedAuditions = Audition::seatsPublished() $publishedAuditions = Audition::seatsPublished()
@ -34,48 +41,59 @@ class ResultsPage extends Controller
->with('seats.entry.student') ->with('seats.entry.student')
->with('event.ensembles') ->with('event.ensembles')
->orderBy('score_order')->get(); ->orderBy('score_order')->get();
$resultsSeatList = Cache::rememberForever('resultsSeatList', function () use ($publishedAuditions) { $resultsSeatList = Cache::rememberForever('resultsSeatList',
$seatList = []; function () use ($publishedAuditions) {
// Load the $seatList in the form of $seatlist[audition_id] is an array of seats for that audition $seatList = [];
// each $seatList[audition_id][] will contain a string with ensemble and seat number and the student object filling it // Load the $seatList in the form of $seatlist[audition_id] is an array of seats for that audition
foreach ($publishedAuditions as $audition) { // each $seatList[audition_id][] will contain a string with ensemble and seat number and the student object filling it
$seats = $audition->seats->groupBy('ensemble_id'); foreach ($publishedAuditions as $audition) {
$ensembles = $audition->event->ensembles; $seats = $audition->seats->groupBy('ensemble_id');
foreach ($ensembles as $ensemble) { $ensembles = $audition->event->ensembles;
if (! $seats->has($ensemble->id)) { // If there are no students seated in this ensemble, skip it foreach ($ensembles as $ensemble) {
continue; if (! $seats->has($ensemble->id)) { // If there are no students seated in this ensemble, skip it
} continue;
foreach ($seats[$ensemble->id] as $seat) { }
/** @var Seat $seat */ foreach ($seats[$ensemble->id] as $seat) {
$seatList[$audition->id][] = [ /** @var Seat $seat */
'seat' => $ensemble->name.' '.$seat->seat, $seatList[$audition->id][] = [
'student' => $seat->entry->student, 'seat' => $ensemble->name.' '.$seat->seat,
]; 'student' => $seat->entry->student,
];
}
} }
} }
}
return $seatList; return $seatList;
}); });
if (auditionSetting('advanceTo')) { if (auditionSetting('advanceTo')) {
$publishedAdvancementAuditions = Audition::advancementPublished()->orderBy('score_order')->get(); $publishedAdvancementAuditions = Audition::advancementPublished()->orderBy('score_order')->get();
$resultsAdvancementList = Cache::rememberForever('resultsAdvancementList',
function () {
// get entries with a related flag of will_advance
$advancingEntries = Entry::forAdvancement()->with('student.school')
->whereHas('flags', function ($query) {
$query->where('flag_name', EntryFlags::WILL_ADVANCE);
})
->get();
$advancingEntries = $advancingEntries->sortBy(function ($entry) {
return $entry->student->full_name(true);
});
return $advancingEntries->groupBy('audition_id'); // get entries with a related flag of will_advance
}); $advancingEntries = Entry::forAdvancement()->with('student.school')
->whereHas('flags', function ($query) {
$query->where('flag_name', EntryFlags::WILL_ADVANCE);
})
->get();
$advancingEntries = $advancingEntries->sortBy(function ($entry) {
return $entry->student->full_name(true);
});
$resultsAdvancementList = $advancingEntries->groupBy('audition_id');
} }
return view('results.index', compact('publishedAuditions', 'resultsSeatList', 'publishedAdvancementAuditions', 'resultsAdvancementList')); $content = View::make('results.index',
compact('publishedAuditions', 'resultsSeatList', 'publishedAdvancementAuditions',
'resultsAdvancementList'))->render();
Cache::forever($cacheKey, $content);
return response($content);
}
private function generateResultsPage()
{
} }
} }

View File

@ -75,6 +75,7 @@ class AdvancementController extends Controller
$entry->addFlag('will_advance'); $entry->addFlag('will_advance');
} }
Cache::forget('audition'.$audition->id.'advancement'); Cache::forget('audition'.$audition->id.'advancement');
Cache::forget('publicResultsPage');
return redirect()->route('advancement.ranking', ['audition' => $audition->id])->with('success', return redirect()->route('advancement.ranking', ['audition' => $audition->id])->with('success',
'Passers have been set successfully'); 'Passers have been set successfully');
@ -87,6 +88,7 @@ class AdvancementController extends Controller
$entry->removeFlag('will_advance'); $entry->removeFlag('will_advance');
} }
Cache::forget('audition'.$audition->id.'advancement'); Cache::forget('audition'.$audition->id.'advancement');
Cache::forget('publicResultsPage');
return redirect()->route('advancement.ranking', ['audition' => $audition->id])->with('success', return redirect()->route('advancement.ranking', ['audition' => $audition->id])->with('success',
'Passers have been cleared successfully'); 'Passers have been cleared successfully');