From 5dd38bd033ab4a6f06e31c33eb8619e7921edc29 Mon Sep 17 00:00:00 2001 From: Kevin Ullyott Date: Tue, 20 Feb 2024 14:49:46 -0500 Subject: [PATCH 1/2] Adjust all cache tags and keys for proper hashmapping Signed-off-by: Kevin Ullyott --- app-modules/alert/src/Observers/AlertObserver.php | 8 ++++---- .../src/Observers/ApplicationSubmissionObserver.php | 2 +- .../form/src/Observers/FormSubmissionObserver.php | 2 +- .../src/Observers/SubscriptionObserver.php | 8 ++++---- .../ProspectResource/Pages/ManageProspectAlerts.php | 2 +- .../Pages/ManageProspectApplicationSubmissions.php | 2 +- .../Pages/ManageProspectFormSubmissions.php | 2 +- .../prospect/src/Filament/Widgets/ProspectStats.php | 12 ++++++------ .../StudentResource/Pages/ManageStudentAlerts.php | 2 +- .../Pages/ManageStudentApplicationSubmissions.php | 2 +- .../Pages/ManageStudentFormSubmissions.php | 2 +- .../src/Filament/Widgets/StudentStats.php | 12 ++++++------ .../tests/Listeners/AddRecordToTimelineTest.php | 2 +- .../tests/Listeners/RemoveRecordFromTimelineTest.php | 2 +- app/Filament/Widgets/ProspectGrowthChart.php | 2 +- 15 files changed, 31 insertions(+), 31 deletions(-) diff --git a/app-modules/alert/src/Observers/AlertObserver.php b/app-modules/alert/src/Observers/AlertObserver.php index 252ed7ff7e..e31afdb501 100644 --- a/app-modules/alert/src/Observers/AlertObserver.php +++ b/app-modules/alert/src/Observers/AlertObserver.php @@ -55,8 +55,8 @@ public function created(Alert $alert): void resolve(SubscriptionCreate::class)->handle($user, $alert->getSubscribable(), false); Cache::tags([match ($alert->concern_type) { - app(Prospect::class)->getMorphClass() => "{user-{$user->getKey()}-prospect-alerts}", - app(Student::class)->getMorphClass() => "{user-{$user->getKey()}-student-alerts}", + app(Prospect::class)->getMorphClass() => "user-{$user->getKey()}-prospect-alerts", + app(Student::class)->getMorphClass() => "user-{$user->getKey()}-student-alerts", }])->flush(); } @@ -77,8 +77,8 @@ public function deleted(Alert $alert): void if ($user) { Cache::tags([match ($alert->concern_type) { - app(Prospect::class)->getMorphClass() => "{user-{$user->getKey()}-prospect-alerts}", - app(Student::class)->getMorphClass() => "{user-{$user->getKey()}-student-alerts}", + app(Prospect::class)->getMorphClass() => "user-{$user->getKey()}-prospect-alerts", + app(Student::class)->getMorphClass() => "user-{$user->getKey()}-student-alerts", }])->flush(); } } diff --git a/app-modules/application/src/Observers/ApplicationSubmissionObserver.php b/app-modules/application/src/Observers/ApplicationSubmissionObserver.php index f90cef6223..7c950eb6d8 100644 --- a/app-modules/application/src/Observers/ApplicationSubmissionObserver.php +++ b/app-modules/application/src/Observers/ApplicationSubmissionObserver.php @@ -61,7 +61,7 @@ public function created(ApplicationSubmission $submission): void if (! is_null($submission->author)) { Cache::tags('{application-submission-count}') ->forget( - "{application-submission-count-{$submission->author->getKey()}}" + "application-submission-count-{$submission->author->getKey()}" ); } } diff --git a/app-modules/form/src/Observers/FormSubmissionObserver.php b/app-modules/form/src/Observers/FormSubmissionObserver.php index 7c900606f5..f13ff5b393 100644 --- a/app-modules/form/src/Observers/FormSubmissionObserver.php +++ b/app-modules/form/src/Observers/FormSubmissionObserver.php @@ -52,7 +52,7 @@ public function created(FormSubmission $submission): void if (! is_null($submission->author)) { Cache::tags('{form-submission-count}') ->forget( - "{form-submission-count-{$submission->author->getKey()}}" + "form-submission-count-{$submission->author->getKey()}" ); } } diff --git a/app-modules/notification/src/Observers/SubscriptionObserver.php b/app-modules/notification/src/Observers/SubscriptionObserver.php index 55e887c96a..3e147efac4 100644 --- a/app-modules/notification/src/Observers/SubscriptionObserver.php +++ b/app-modules/notification/src/Observers/SubscriptionObserver.php @@ -55,8 +55,8 @@ public function created(Subscription $subscription): void if ($user) { Cache::tags([match ($subscription->subscribable_type) { - app(Prospect::class)->getMorphClass() => "{user-{$user->getKey()}-prospect-subscriptions‘", - app(Student::class)->getMorphClass() => "{user-{$user->getKey()}-student-subscriptions‘", + app(Prospect::class)->getMorphClass() => "user-{$user->getKey()}-prospect-subscriptions", + app(Student::class)->getMorphClass() => "user-{$user->getKey()}-student-subscriptions", }])->flush(); } } @@ -70,8 +70,8 @@ public function deleted(Subscription $subscription): void if ($user) { Cache::tags([match ($subscription->subscribable_type) { - app(Prospect::class)->getMorphClass() => "{user-{$user->getKey()}-prospect-subscriptions‘", - app(Student::class)->getMorphClass() => "{user-{$user->getKey()}-student-subscriptions‘", + app(Prospect::class)->getMorphClass() => "user-{$user->getKey()}-prospect-subscriptions", + app(Student::class)->getMorphClass() => "user-{$user->getKey()}-student-subscriptions", }])->flush(); } } diff --git a/app-modules/prospect/src/Filament/Resources/ProspectResource/Pages/ManageProspectAlerts.php b/app-modules/prospect/src/Filament/Resources/ProspectResource/Pages/ManageProspectAlerts.php index a3a16e8f03..7b90e0a614 100644 --- a/app-modules/prospect/src/Filament/Resources/ProspectResource/Pages/ManageProspectAlerts.php +++ b/app-modules/prospect/src/Filament/Resources/ProspectResource/Pages/ManageProspectAlerts.php @@ -81,7 +81,7 @@ public static function getNavigationItems(array $urlParameters = []): array /** @var Prospect $ownerRecord */ $alertsCount = Cache::tags('{alert-count}') ->remember( - "{alert-count-{$ownerRecord->getKey()}}", + "alert-count-{$ownerRecord->getKey()}", now()->addMinutes(5), function () use ($ownerRecord): int { return $ownerRecord->alerts()->status(AlertStatus::Active)->count(); diff --git a/app-modules/prospect/src/Filament/Resources/ProspectResource/Pages/ManageProspectApplicationSubmissions.php b/app-modules/prospect/src/Filament/Resources/ProspectResource/Pages/ManageProspectApplicationSubmissions.php index 87259eb408..cf446fc7f5 100644 --- a/app-modules/prospect/src/Filament/Resources/ProspectResource/Pages/ManageProspectApplicationSubmissions.php +++ b/app-modules/prospect/src/Filament/Resources/ProspectResource/Pages/ManageProspectApplicationSubmissions.php @@ -135,7 +135,7 @@ public static function getNavigationItems(array $urlParameters = []): array /** @var Prospect $ownerRecord */ $applicationSubmissionsCount = Cache::tags('{application-submission-count}') ->remember( - "{application-submission-count-{$ownerRecord->getKey()}}", + "application-submission-count-{$ownerRecord->getKey()}", now()->addMinutes(5), function () use ($ownerRecord): int { return $ownerRecord->applicationSubmissions()->count(); diff --git a/app-modules/prospect/src/Filament/Resources/ProspectResource/Pages/ManageProspectFormSubmissions.php b/app-modules/prospect/src/Filament/Resources/ProspectResource/Pages/ManageProspectFormSubmissions.php index 198e1d805b..a93eecbbd2 100644 --- a/app-modules/prospect/src/Filament/Resources/ProspectResource/Pages/ManageProspectFormSubmissions.php +++ b/app-modules/prospect/src/Filament/Resources/ProspectResource/Pages/ManageProspectFormSubmissions.php @@ -117,7 +117,7 @@ public static function getNavigationItems(array $urlParameters = []): array /** @var Prospect $ownerRecord */ $formSubmissionsCount = Cache::tags('{form-submission-count}') ->remember( - "{form-submission-count-{$ownerRecord->getKey()}}", + "form-submission-count-{$ownerRecord->getKey()}", now()->addMinutes(5), function () use ($ownerRecord): int { return $ownerRecord->formSubmissions()->count(); diff --git a/app-modules/prospect/src/Filament/Widgets/ProspectStats.php b/app-modules/prospect/src/Filament/Widgets/ProspectStats.php index 2a370cb76a..57a3b639c1 100644 --- a/app-modules/prospect/src/Filament/Widgets/ProspectStats.php +++ b/app-modules/prospect/src/Filament/Widgets/ProspectStats.php @@ -55,21 +55,21 @@ protected function getStats(): array return [ Stat::make('Prospects', Number::abbreviate( Cache::tags(['{prospects}']) - ->remember('{prospects-count}', now()->addHour(), function (): int { + ->remember('prospects-count', now()->addHour(), function (): int { return Prospect::count(); }), maxPrecision: 2, )), - Stat::make('Subscriptions', Cache::tags(['{prospects}', "{user-{$user->getKey()}-prospect-subscriptions}"]) - ->remember("{user-{$user->getKey()}-prospect-subscriptions-count}", now()->addHour(), function () use ($user): int { + Stat::make('Subscriptions', Cache::tags(['{prospects}', "user-{$user->getKey()}-prospect-subscriptions"]) + ->remember("user-{$user->getKey()}-prospect-subscriptions-count", now()->addHour(), function () use ($user): int { return $user->prospectSubscriptions()->count(); })), - Stat::make('Alerts', Cache::tags(['{prospects}', "{user-{$user->getKey()}-prospect-alerts}"]) - ->remember("{user-{$user->getKey()}-prospect-alerts-count}", now()->addHour(), function () use ($user): int { + Stat::make('Alerts', Cache::tags(['{prospects}', "user-{$user->getKey()}-prospect-alerts"]) + ->remember("user-{$user->getKey()}-prospect-alerts-count", now()->addHour(), function () use ($user): int { return $user->prospectAlerts()->status(AlertStatus::Active)->count(); })), Stat::make('Caseloads', Cache::tags(["{user-{$user->getKey()}-prospect-caseloads}"]) - ->remember("{user-{$user->getKey()}-prospect-caseloads-count}", now()->addHour(), function () use ($user): int { + ->remember("user-{$user->getKey()}-prospect-caseloads-count", now()->addHour(), function () use ($user): int { return $user->caseloads()->model(CaseloadModel::Prospect)->count(); })), ]; diff --git a/app-modules/student-data-model/src/Filament/Resources/StudentResource/Pages/ManageStudentAlerts.php b/app-modules/student-data-model/src/Filament/Resources/StudentResource/Pages/ManageStudentAlerts.php index 3dfbffce9d..5a5b3bd543 100644 --- a/app-modules/student-data-model/src/Filament/Resources/StudentResource/Pages/ManageStudentAlerts.php +++ b/app-modules/student-data-model/src/Filament/Resources/StudentResource/Pages/ManageStudentAlerts.php @@ -81,7 +81,7 @@ public static function getNavigationItems(array $urlParameters = []): array /** @var Student $ownerRecord */ $alertsCount = Cache::tags('{alert-count}') ->remember( - "{alert-count-{$ownerRecord->getKey()}}", + "alert-count-{$ownerRecord->getKey()}", now()->addMinutes(5), function () use ($ownerRecord): int { return $ownerRecord->alerts()->status(AlertStatus::Active)->count(); diff --git a/app-modules/student-data-model/src/Filament/Resources/StudentResource/Pages/ManageStudentApplicationSubmissions.php b/app-modules/student-data-model/src/Filament/Resources/StudentResource/Pages/ManageStudentApplicationSubmissions.php index 3e64575f91..3f39b76cae 100644 --- a/app-modules/student-data-model/src/Filament/Resources/StudentResource/Pages/ManageStudentApplicationSubmissions.php +++ b/app-modules/student-data-model/src/Filament/Resources/StudentResource/Pages/ManageStudentApplicationSubmissions.php @@ -135,7 +135,7 @@ public static function getNavigationItems(array $urlParameters = []): array /** @var Student $ownerRecord */ $applicationSubmissionsCount = Cache::tags('{application-submission-count}') ->remember( - "{application-submission-count-{$ownerRecord->getKey()}}", + "application-submission-count-{$ownerRecord->getKey()}", now()->addMinutes(5), function () use ($ownerRecord): int { return $ownerRecord->applicationSubmissions()->count(); diff --git a/app-modules/student-data-model/src/Filament/Resources/StudentResource/Pages/ManageStudentFormSubmissions.php b/app-modules/student-data-model/src/Filament/Resources/StudentResource/Pages/ManageStudentFormSubmissions.php index 030cb17c77..4e6ab6cdd9 100644 --- a/app-modules/student-data-model/src/Filament/Resources/StudentResource/Pages/ManageStudentFormSubmissions.php +++ b/app-modules/student-data-model/src/Filament/Resources/StudentResource/Pages/ManageStudentFormSubmissions.php @@ -117,7 +117,7 @@ public static function getNavigationItems(array $urlParameters = []): array /** @var Prospect $ownerRecord */ $formSubmissionsCount = Cache::tags('{form-submission-count}') ->remember( - "{form-submission-count-{$ownerRecord->getKey()}}", + "form-submission-count-{$ownerRecord->getKey()}", now()->addMinutes(5), function () use ($ownerRecord): int { return $ownerRecord->formSubmissions()->count(); diff --git a/app-modules/student-data-model/src/Filament/Widgets/StudentStats.php b/app-modules/student-data-model/src/Filament/Widgets/StudentStats.php index 9fbba6caeb..48ee593f22 100644 --- a/app-modules/student-data-model/src/Filament/Widgets/StudentStats.php +++ b/app-modules/student-data-model/src/Filament/Widgets/StudentStats.php @@ -55,21 +55,21 @@ protected function getStats(): array return [ Stat::make('Students', Number::abbreviate( Cache::tags(['{students}']) - ->remember('{students-count}', now()->addHour(), function (): int { + ->remember('students-count', now()->addHour(), function (): int { return Student::count(); }), maxPrecision: 2, )), - Stat::make('Subscriptions', Cache::tags(['{students', "{user-{$user->getKey()}-student-subscriptions}"]) - ->remember("{user-{$user->getKey()}-student-subscriptions-count}", now()->addHour(), function () use ($user): int { + Stat::make('Subscriptions', Cache::tags(['{students', "user-{$user->getKey()}-student-subscriptions"]) + ->remember("user-{$user->getKey()}-student-subscriptions-count", now()->addHour(), function () use ($user): int { return $user->studentSubscriptions()->count(); })), - Stat::make('Alerts', Cache::tags(['{students', "{user-{$user->getKey()}-student-alerts}"]) - ->remember("{user-{$user->getKey()}-student-alerts-count}", now()->addHour(), function () use ($user): int { + Stat::make('Alerts', Cache::tags(['{students}', "user-{$user->getKey()}-student-alerts"]) + ->remember("user-{$user->getKey()}-student-alerts-count", now()->addHour(), function () use ($user): int { return $user->studentAlerts()->status(AlertStatus::Active)->count(); })), Stat::make('Caseloads', Cache::tags(["{user-{$user->getKey()}-student-caseloads}"]) - ->remember("{user-{$user->getKey()}-student-caseloads-count}", now()->addHour(), function () use ($user): int { + ->remember("user-{$user->getKey()}-student-caseloads-count", now()->addHour(), function () use ($user): int { return $user->caseloads()->model(CaseloadModel::Student)->count(); })), ]; diff --git a/app-modules/timeline/tests/Listeners/AddRecordToTimelineTest.php b/app-modules/timeline/tests/Listeners/AddRecordToTimelineTest.php index 87c117f06f..9c795e95c1 100644 --- a/app-modules/timeline/tests/Listeners/AddRecordToTimelineTest.php +++ b/app-modules/timeline/tests/Listeners/AddRecordToTimelineTest.php @@ -64,7 +64,7 @@ // The cache key for the educatable should be busted expect(Cache::has( - "{timeline.synced.{$initialResponse->sender->getMorphClass()}.{$initialResponse->sender->getKey()}}" + "timeline.synced.{$initialResponse->sender->getMorphClass()}.{$initialResponse->sender->getKey()}" ))->toBeFalse(); }); diff --git a/app-modules/timeline/tests/Listeners/RemoveRecordFromTimelineTest.php b/app-modules/timeline/tests/Listeners/RemoveRecordFromTimelineTest.php index 380eaf5770..df41581d8e 100644 --- a/app-modules/timeline/tests/Listeners/RemoveRecordFromTimelineTest.php +++ b/app-modules/timeline/tests/Listeners/RemoveRecordFromTimelineTest.php @@ -66,7 +66,7 @@ // The cache key for the educatable should be busted expect(Cache::has( - "{timeline.synced.{$initialResponse->sender->getMorphClass()}.{$initialResponse->sender->getKey()}}" + "timeline.synced.{$initialResponse->sender->getMorphClass()}.{$initialResponse->sender->getKey()}" ))->toBeFalse(); }); diff --git a/app/Filament/Widgets/ProspectGrowthChart.php b/app/Filament/Widgets/ProspectGrowthChart.php index e8df899fe0..3fec85b8af 100644 --- a/app/Filament/Widgets/ProspectGrowthChart.php +++ b/app/Filament/Widgets/ProspectGrowthChart.php @@ -68,7 +68,7 @@ protected function getOptions(): array protected function getData(): array { $runningTotalPerMonth = Cache::tags(['{prospects}']) - ->remember('{prospect-growth-chart-data}', now()->addHour(), function (): array { + ->remember('prospect-growth-chart-data', now()->addHour(), function (): array { $totalCreatedPerMonth = Prospect::query() ->toBase() ->selectRaw('date_trunc(\'month\', created_at) as month') From c000553638c267a81d3264477005b0cff693f59f Mon Sep 17 00:00:00 2001 From: Kevin Ullyott Date: Tue, 20 Feb 2024 15:41:57 -0500 Subject: [PATCH 2/2] Add s6 command to clear the cache Signed-off-by: Kevin Ullyott --- .../s6-rc.d/clear-cache/dependencies | 1 + docker/s6-overlay/s6-rc.d/clear-cache/type | 1 + docker/s6-overlay/s6-rc.d/clear-cache/up | 1 + docker/s6-overlay/scripts/clear-cache | 19 +++++++++++++++++++ docker/s6-overlay/user/clear-cache | 0 5 files changed, 22 insertions(+) create mode 100755 docker/s6-overlay/s6-rc.d/clear-cache/dependencies create mode 100755 docker/s6-overlay/s6-rc.d/clear-cache/type create mode 100755 docker/s6-overlay/s6-rc.d/clear-cache/up create mode 100755 docker/s6-overlay/scripts/clear-cache create mode 100755 docker/s6-overlay/user/clear-cache diff --git a/docker/s6-overlay/s6-rc.d/clear-cache/dependencies b/docker/s6-overlay/s6-rc.d/clear-cache/dependencies new file mode 100755 index 0000000000..cfd833db1c --- /dev/null +++ b/docker/s6-overlay/s6-rc.d/clear-cache/dependencies @@ -0,0 +1 @@ +runas-user \ No newline at end of file diff --git a/docker/s6-overlay/s6-rc.d/clear-cache/type b/docker/s6-overlay/s6-rc.d/clear-cache/type new file mode 100755 index 0000000000..3d92b15f2d --- /dev/null +++ b/docker/s6-overlay/s6-rc.d/clear-cache/type @@ -0,0 +1 @@ +oneshot \ No newline at end of file diff --git a/docker/s6-overlay/s6-rc.d/clear-cache/up b/docker/s6-overlay/s6-rc.d/clear-cache/up new file mode 100755 index 0000000000..f994c5c4d9 --- /dev/null +++ b/docker/s6-overlay/s6-rc.d/clear-cache/up @@ -0,0 +1 @@ +/etc/s6-overlay/scripts/clear-cache diff --git a/docker/s6-overlay/scripts/clear-cache b/docker/s6-overlay/scripts/clear-cache new file mode 100755 index 0000000000..dfa33b6008 --- /dev/null +++ b/docker/s6-overlay/scripts/clear-cache @@ -0,0 +1,19 @@ +#!/command/with-contenv bash + +# Exit on error +set -e + +# Check to see if an Artisan file exists and assume it means Laravel is configured. +if [ -f $WEBUSER_HOME/artisan ] && [ ${CLEAR_CACHE:="false"} == "true" ]; then + echo "Clearing the cache..." + + USERNAME=$(id -nu $PUID) + + s6-setuidgid $USERNAME php $WEBUSER_HOME/artisan cache:clear + + echo "Cache Cleared!" +else + echo "Skipping clearing the cache because we could not detect a Laravel install or it was specifically not enabled..." +fi + +exit 0 diff --git a/docker/s6-overlay/user/clear-cache b/docker/s6-overlay/user/clear-cache new file mode 100755 index 0000000000..e69de29bb2