Add entry cache and update observers

This commit is contained in:
Matt Young 2024-06-16 00:16:24 -05:00
parent 01c0365b5d
commit a080e35c8b
10 changed files with 156 additions and 4 deletions

View File

@ -0,0 +1,36 @@
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class EntryChange
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public bool $auditionId;
/**
* Create a new event instance.
*/
public function __construct($auditionId = null)
{
$this->auditionId = $auditionId;
}
/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel('channel-name'),
];
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace App\Listeners;
use App\Events\AuditionChange;
use App\Events\EntryChange;
use App\Services\AuditionCacheService;
use App\Services\EntryCacheService;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class RefreshEntryCache
{
protected $entryCacheService;
/**
* Create the event listener.
*/
public function __construct(EntryCacheService $cacheService)
{
$this->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.

View File

@ -3,6 +3,7 @@
namespace App\Observers; namespace App\Observers;
use App\Events\AuditionChange; use App\Events\AuditionChange;
use App\Events\EntryChange;
use App\Models\Audition; use App\Models\Audition;
class AuditionObserver class AuditionObserver
@ -13,6 +14,7 @@ class AuditionObserver
public function created(Audition $audition): void public function created(Audition $audition): void
{ {
AuditionChange::dispatch(); AuditionChange::dispatch();
EntryChange::dispatch($audition->id);
} }
/** /**
@ -21,6 +23,7 @@ class AuditionObserver
public function updated(Audition $audition): void public function updated(Audition $audition): void
{ {
AuditionChange::dispatch(); AuditionChange::dispatch();
EntryChange::dispatch($audition->id);
} }
/** /**
@ -29,6 +32,7 @@ class AuditionObserver
public function deleted(Audition $audition): void public function deleted(Audition $audition): void
{ {
AuditionChange::dispatch(); AuditionChange::dispatch();
EntryChange::dispatch($audition->id);
} }
/** /**
@ -37,6 +41,7 @@ class AuditionObserver
public function restored(Audition $audition): void public function restored(Audition $audition): void
{ {
AuditionChange::dispatch(); AuditionChange::dispatch();
EntryChange::dispatch($audition->id);
} }
/** /**
@ -45,5 +50,6 @@ class AuditionObserver
public function forceDeleted(Audition $audition): void public function forceDeleted(Audition $audition): void
{ {
AuditionChange::dispatch(); AuditionChange::dispatch();
EntryChange::dispatch($audition->id);
} }
} }

View File

@ -3,6 +3,7 @@
namespace App\Observers; namespace App\Observers;
use App\Events\AuditionChange; use App\Events\AuditionChange;
use App\Events\EntryChange;
use App\Models\Entry; use App\Models\Entry;
class EntryObserver class EntryObserver
@ -13,6 +14,7 @@ class EntryObserver
public function created(Entry $entry): void public function created(Entry $entry): void
{ {
AuditionChange::dispatch(); AuditionChange::dispatch();
EntryChange::dispatch($entry->audition_id);
} }
/** /**
@ -21,6 +23,7 @@ class EntryObserver
public function updated(Entry $entry): void public function updated(Entry $entry): void
{ {
AuditionChange::dispatch(); AuditionChange::dispatch();
EntryChange::dispatch($entry->audition_id);
} }
/** /**
@ -29,6 +32,7 @@ class EntryObserver
public function deleted(Entry $entry): void public function deleted(Entry $entry): void
{ {
AuditionChange::dispatch(); AuditionChange::dispatch();
EntryChange::dispatch($entry->audition_id);
} }
/** /**
@ -37,6 +41,7 @@ class EntryObserver
public function restored(Entry $entry): void public function restored(Entry $entry): void
{ {
AuditionChange::dispatch(); AuditionChange::dispatch();
EntryChange::dispatch($entry->audition_id);
} }
/** /**
@ -44,6 +49,6 @@ class EntryObserver
*/ */
public function forceDeleted(Entry $entry): void public function forceDeleted(Entry $entry): void
{ {
// EntryChange::dispatch($entry->audition_id);
} }
} }

View File

@ -3,6 +3,7 @@
namespace App\Observers; namespace App\Observers;
use App\Events\AuditionChange; use App\Events\AuditionChange;
use App\Events\EntryChange;
use App\Models\School; use App\Models\School;
class SchoolObserver class SchoolObserver
@ -21,6 +22,7 @@ class SchoolObserver
public function updated(School $school): void public function updated(School $school): void
{ {
AuditionChange::dispatch(); AuditionChange::dispatch();
EntryChange::dispatch();
} }
/** /**

View File

@ -3,6 +3,7 @@
namespace App\Observers; namespace App\Observers;
use App\Events\AuditionChange; use App\Events\AuditionChange;
use App\Events\EntryChange;
use App\Models\Student; use App\Models\Student;
class StudentObserver class StudentObserver
@ -21,6 +22,7 @@ class StudentObserver
public function updated(Student $student): void public function updated(Student $student): void
{ {
AuditionChange::dispatch(); AuditionChange::dispatch();
EntryChange::dispatch();
} }
/** /**

View File

@ -26,6 +26,7 @@ use App\Observers\SubscoreDefinitionObserver;
use App\Observers\UserObserver; use App\Observers\UserObserver;
use App\Services\AuditionCacheService; use App\Services\AuditionCacheService;
use App\Services\DoublerService; use App\Services\DoublerService;
use App\Services\EntryCacheService;
use App\Services\TabulationService; use App\Services\TabulationService;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
@ -41,10 +42,16 @@ class AppServiceProvider extends ServiceProvider
return new AuditionCacheService(); return new AuditionCacheService();
}); });
$this->app->singleton(EntryCacheService::class, function($app) {
return new EntryCacheService($app->make(AuditionCacheService::class));
});
$this->app->singleton(TabulationService::class, function($app) { $this->app->singleton(TabulationService::class, function($app) {
return new TabulationService($app->make(AuditionCacheService::class)); return new TabulationService($app->make(AuditionCacheService::class));
}); });
$this->app->singleton(DoublerService::class, function($app) { $this->app->singleton(DoublerService::class, function($app) {
return new DoublerService($app->make(AuditionCacheService::class),$app->make(TabulationService::class)); return new DoublerService($app->make(AuditionCacheService::class),$app->make(TabulationService::class));
}); });

View File

@ -27,7 +27,7 @@ class AuditionCacheService
*/ */
public function getAuditions(): \Illuminate\Database\Eloquent\Collection 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'); if (App::environment('local')) Session::flash('success','Audition Cache Updated');
return Audition::with(['scoringGuide.subscores','judges']) return Audition::with(['scoringGuide.subscores','judges'])
->withCount('judges') ->withCount('judges')

View File

@ -0,0 +1,54 @@
<?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);
}
}
}

View File

@ -9,11 +9,14 @@
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session; use Illuminate\Support\Facades\Session;
@endphp @endphp
@inject('tabservice','App\Services\TabulationService'); @inject('entryservice','App\Services\EntryCacheService');
<x-layout.app> <x-layout.app>
<x-slot:page_title>Test Page</x-slot:page_title> <x-slot:page_title>Test Page</x-slot:page_title>
@php
$e = $entryservice->getEntries();
dd($e);
@endphp
@php(dd($tabservice->entryScoreSheetsAreValid(Entry::find(1102))))
@foreach($auditions as $audition) @foreach($auditions as $audition)
{{ $audition->name }} has {{ $audition->entries_count }} entries. {{ $audition->scored_entries_count }} are {{ $audition->name }} has {{ $audition->entries_count }} entries. {{ $audition->scored_entries_count }} are