auditionadmin/app/Services/AuditionCacheService.php

58 lines
1.5 KiB
PHP

<?php
namespace App\Services;
use App\Models\Audition;
use App\Models\ScoringGuide;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Session;
class AuditionCacheService
{
protected $cacheKey = 'auditions';
/**
* Create a new class instance.
*/
public function __construct()
{
//
}
/**
* Return or fill cache of auditions including the audition,
* scoringGuide.subscores, judges, judges_count, and entries_count
* @return Collection
*/
public function getAuditions(): \Illuminate\Database\Eloquent\Collection
{
return Cache::remember($this->cacheKey, 3600, function () {
if (App::environment('local')) Session::flash('success','Audition Cache Updated');
return Audition::with(['scoringGuide.subscores','judges'])
->withCount('judges')
->withCount('entries')
->orderBy('score_order')
->get()
->keyBy('id');
});
}
public function getAudition($id): Audition
{
return $this->getAuditions()->firstWhere('id',$id);
}
public function refreshCache()
{
Cache::forget($this->cacheKey);
$this->getAuditions();
}
public function clearCache()
{
if (App::environment('local')) Session::flash('success','Audition Cache Cleared');
Cache::forget($this->cacheKey);
}
}