diff --git a/app/Events/EntryChange.php b/app/Events/EntryChange.php new file mode 100644 index 0000000..4471dc0 --- /dev/null +++ b/app/Events/EntryChange.php @@ -0,0 +1,36 @@ +auditionId = $auditionId; + } + + /** + * Get the channels the event should broadcast on. + * + * @return array + */ + public function broadcastOn(): array + { + return [ + new PrivateChannel('channel-name'), + ]; + } +} diff --git a/app/Listeners/RefreshEntryCache.php b/app/Listeners/RefreshEntryCache.php new file mode 100644 index 0000000..156c7f4 --- /dev/null +++ b/app/Listeners/RefreshEntryCache.php @@ -0,0 +1,37 @@ +entryCacheService = $cacheService; + } + + /** + * Handle the event. + */ + public function handle(EntryChange $auditionId): void + { + if ($auditionId) { + $this->entryCacheService->clearEntryCacheForAudition($auditionId); + } else { + $this->entryCacheService->clearEntryCaches(); + } + } +} + + +//TODO To give your application a speed boost, you should cache a manifest of all of your application's listeners using the optimize or event:cache Artisan commands. Typically, this command should be run as part of your application's deployment process. This manifest will be used by the framework to speed up the event registration process. The event:clear command may be used to destroy the event cache. diff --git a/app/Observers/AuditionObserver.php b/app/Observers/AuditionObserver.php index 4748957..27d139f 100644 --- a/app/Observers/AuditionObserver.php +++ b/app/Observers/AuditionObserver.php @@ -3,6 +3,7 @@ namespace App\Observers; use App\Events\AuditionChange; +use App\Events\EntryChange; use App\Models\Audition; class AuditionObserver @@ -13,6 +14,7 @@ class AuditionObserver public function created(Audition $audition): void { AuditionChange::dispatch(); + EntryChange::dispatch($audition->id); } /** @@ -21,6 +23,7 @@ class AuditionObserver public function updated(Audition $audition): void { AuditionChange::dispatch(); + EntryChange::dispatch($audition->id); } /** @@ -29,6 +32,7 @@ class AuditionObserver public function deleted(Audition $audition): void { AuditionChange::dispatch(); + EntryChange::dispatch($audition->id); } /** @@ -37,6 +41,7 @@ class AuditionObserver public function restored(Audition $audition): void { AuditionChange::dispatch(); + EntryChange::dispatch($audition->id); } /** @@ -45,5 +50,6 @@ class AuditionObserver public function forceDeleted(Audition $audition): void { AuditionChange::dispatch(); + EntryChange::dispatch($audition->id); } } diff --git a/app/Observers/EntryObserver.php b/app/Observers/EntryObserver.php index 27c193a..67a51cf 100644 --- a/app/Observers/EntryObserver.php +++ b/app/Observers/EntryObserver.php @@ -3,6 +3,7 @@ namespace App\Observers; use App\Events\AuditionChange; +use App\Events\EntryChange; use App\Models\Entry; class EntryObserver @@ -13,6 +14,7 @@ class EntryObserver public function created(Entry $entry): void { AuditionChange::dispatch(); + EntryChange::dispatch($entry->audition_id); } /** @@ -21,6 +23,7 @@ class EntryObserver public function updated(Entry $entry): void { AuditionChange::dispatch(); + EntryChange::dispatch($entry->audition_id); } /** @@ -29,6 +32,7 @@ class EntryObserver public function deleted(Entry $entry): void { AuditionChange::dispatch(); + EntryChange::dispatch($entry->audition_id); } /** @@ -37,6 +41,7 @@ class EntryObserver public function restored(Entry $entry): void { AuditionChange::dispatch(); + EntryChange::dispatch($entry->audition_id); } /** @@ -44,6 +49,6 @@ class EntryObserver */ public function forceDeleted(Entry $entry): void { - // + EntryChange::dispatch($entry->audition_id); } } diff --git a/app/Observers/SchoolObserver.php b/app/Observers/SchoolObserver.php index c9b0693..a4b7ff0 100644 --- a/app/Observers/SchoolObserver.php +++ b/app/Observers/SchoolObserver.php @@ -3,6 +3,7 @@ namespace App\Observers; use App\Events\AuditionChange; +use App\Events\EntryChange; use App\Models\School; class SchoolObserver @@ -21,6 +22,7 @@ class SchoolObserver public function updated(School $school): void { AuditionChange::dispatch(); + EntryChange::dispatch(); } /** diff --git a/app/Observers/StudentObserver.php b/app/Observers/StudentObserver.php index ad3d718..d9af204 100644 --- a/app/Observers/StudentObserver.php +++ b/app/Observers/StudentObserver.php @@ -3,6 +3,7 @@ namespace App\Observers; use App\Events\AuditionChange; +use App\Events\EntryChange; use App\Models\Student; class StudentObserver @@ -21,6 +22,7 @@ class StudentObserver public function updated(Student $student): void { AuditionChange::dispatch(); + EntryChange::dispatch(); } /** diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 2ca8fdd..67c15ea 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -26,6 +26,7 @@ use App\Observers\SubscoreDefinitionObserver; use App\Observers\UserObserver; use App\Services\AuditionCacheService; use App\Services\DoublerService; +use App\Services\EntryCacheService; use App\Services\TabulationService; use Illuminate\Support\Facades\Event; use Illuminate\Support\ServiceProvider; @@ -41,10 +42,16 @@ class AppServiceProvider extends ServiceProvider return new AuditionCacheService(); }); + $this->app->singleton(EntryCacheService::class, function($app) { + return new EntryCacheService($app->make(AuditionCacheService::class)); + }); + $this->app->singleton(TabulationService::class, function($app) { return new TabulationService($app->make(AuditionCacheService::class)); }); + + $this->app->singleton(DoublerService::class, function($app) { return new DoublerService($app->make(AuditionCacheService::class),$app->make(TabulationService::class)); }); diff --git a/app/Services/AuditionCacheService.php b/app/Services/AuditionCacheService.php index 2b5d859..1a4a908 100644 --- a/app/Services/AuditionCacheService.php +++ b/app/Services/AuditionCacheService.php @@ -27,7 +27,7 @@ class AuditionCacheService */ public function getAuditions(): \Illuminate\Database\Eloquent\Collection { - return Cache::rememberForever($this->cacheKey, function () { + 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') diff --git a/app/Services/EntryCacheService.php b/app/Services/EntryCacheService.php new file mode 100644 index 0000000..3324028 --- /dev/null +++ b/app/Services/EntryCacheService.php @@ -0,0 +1,54 @@ +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); + } + } +} diff --git a/resources/views/test.blade.php b/resources/views/test.blade.php index 783dade..de0e37c 100644 --- a/resources/views/test.blade.php +++ b/resources/views/test.blade.php @@ -9,11 +9,14 @@ use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Session; @endphp -@inject('tabservice','App\Services\TabulationService'); +@inject('entryservice','App\Services\EntryCacheService'); Test Page + @php + $e = $entryservice->getEntries(); + dd($e); + @endphp - @php(dd($tabservice->entryScoreSheetsAreValid(Entry::find(1102)))) @foreach($auditions as $audition) {{ $audition->name }} has {{ $audition->entries_count }} entries. {{ $audition->scored_entries_count }} are