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');
Cache::forget('resultsSeatList');
Cache::forget('publicResultsPage');
}
}

View File

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

View File

@ -10,6 +10,7 @@ use App\Models\Seat;
use App\Services\AuditionService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\View;
use function auditionSetting;
@ -27,6 +28,12 @@ class ResultsPage extends Controller
*/
public function __invoke(Request $request)
{
$cacheKey = 'publicResultsPage';
if (Cache::has($cacheKey)) {
return response(Cache::get($cacheKey));
}
$publishedAdvancementAuditions = [];
$resultsAdvancementList = [];
$publishedAuditions = Audition::seatsPublished()
@ -34,7 +41,8 @@ class ResultsPage extends Controller
->with('seats.entry.student')
->with('event.ensembles')
->orderBy('score_order')->get();
$resultsSeatList = Cache::rememberForever('resultsSeatList', function () use ($publishedAuditions) {
$resultsSeatList = Cache::rememberForever('resultsSeatList',
function () use ($publishedAuditions) {
$seatList = [];
// Load the $seatList in the form of $seatlist[audition_id] is an array of seats for that audition
// each $seatList[audition_id][] will contain a string with ensemble and seat number and the student object filling it
@ -60,8 +68,7 @@ class ResultsPage extends Controller
if (auditionSetting('advanceTo')) {
$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) {
@ -72,10 +79,21 @@ class ResultsPage extends Controller
return $entry->student->full_name(true);
});
return $advancingEntries->groupBy('audition_id');
});
$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');
}
Cache::forget('audition'.$audition->id.'advancement');
Cache::forget('publicResultsPage');
return redirect()->route('advancement.ranking', ['audition' => $audition->id])->with('success',
'Passers have been set successfully');
@ -87,6 +88,7 @@ class AdvancementController extends Controller
$entry->removeFlag('will_advance');
}
Cache::forget('audition'.$audition->id.'advancement');
Cache::forget('publicResultsPage');
return redirect()->route('advancement.ranking', ['audition' => $audition->id])->with('success',
'Passers have been cleared successfully');