auditionadmin/app/Services/EntryCacheService.php

55 lines
1.4 KiB
PHP

<?php
namespace App\Services;
use App\Models\Entry;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
class EntryCacheService
{
protected $auditionCache;
/**
* Create a new class instance.
*/
public function __construct(AuditionCacheService $auditionCache)
{
$this->auditionCache = $auditionCache;
}
public function getEntriesForAudition($auditionId) {
$cacheKey = 'audition' . $auditionId . 'entries';
return Cache::remember($cacheKey, 3600, function () use ($auditionId) {
return Entry::where('audition_id',$auditionId)
->with('student.school')
->get()
->keyBy('id');
});
}
public function getEntries(): Collection
{
$auditions = $this->auditionCache->getAuditions();
$allEntries = [];
foreach ($auditions as $audition) {
$allEntries[$audition->id] = $this->getEntriesForAudition($audition->id);
}
return collect($allEntries);
}
public function clearEntryCacheForAudition($auditionId): void
{
$cacheKey = 'audition' . $auditionId . 'entries';
Cache::forget($cacheKey);
}
public function clearEntryCaches(): void
{
$auditions = $this->auditionCache->getAuditions();
foreach ($auditions as $audition) {
$this->clearEntryCacheForAudition($audition->id);
}
}
}