diff --git a/app/Helpers/CacheHelper.php b/app/Helpers/CacheHelper.php index 9c4c28e..673fd52 100644 --- a/app/Helpers/CacheHelper.php +++ b/app/Helpers/CacheHelper.php @@ -16,5 +16,7 @@ public static function clearHomePage(?string $course = null): void Cache::forget('home_page_subjects_hsc'); Cache::forget('home_page_subjects_ssc'); + Cache::forget('home_page_featured_blogs'); + Cache::forget('home_page_notice'); } } diff --git a/app/Http/Controllers/SubjectController.php b/app/Http/Controllers/SubjectController.php index c1e5f51..e66b6d2 100644 --- a/app/Http/Controllers/SubjectController.php +++ b/app/Http/Controllers/SubjectController.php @@ -25,16 +25,20 @@ public function index($course) ->toArray(); }); - $featuredBlogs = Blog::where('is_featured', true) - ->where('is_published', true) - ->with('user:id,name,username') - ->withCount(['reactions', 'comments']) - ->inRandomOrder() - ->limit(3) - ->get() - ->toArray(); + $featuredBlogs = Cache::remember('home_page_featured_blogs', now()->addDay(), function () { + return Blog::where('is_featured', true) + ->where('is_published', true) + ->with('user:id,name,username') + ->withCount(['reactions', 'comments']) + ->inRandomOrder() + ->limit(3) + ->get() + ->toArray(); + }); - $notice = Notice::activeForDisplay()?->toArray(); + $notice = Cache::rememberForever('home_page_notice', function () { + return Notice::activeForDisplay()?->toArray(); + }); return Inertia::render('Home', [ 'subjects' => $subjects, diff --git a/app/Observers/NoticeObserver.php b/app/Observers/NoticeObserver.php index 84d759e..6b87993 100644 --- a/app/Observers/NoticeObserver.php +++ b/app/Observers/NoticeObserver.php @@ -11,4 +11,9 @@ public function saved(Notice $notice): void { CacheHelper::clearHomePage(); } + + public function deleted(Notice $notice): void + { + CacheHelper::clearHomePage(); + } } diff --git a/tests/Feature/PublicPagesTest.php b/tests/Feature/PublicPagesTest.php index 845808f..a2ccdcf 100644 --- a/tests/Feature/PublicPagesTest.php +++ b/tests/Feature/PublicPagesTest.php @@ -1,11 +1,50 @@ get('/'); $response->assertStatus(200); }); +test('featured blogs and notice are cached on homepage', function () { + $user = User::factory()->create(); + $blog = Blog::factory()->create([ + 'user_id' => $user->id, + 'is_featured' => true, + 'is_published' => true, + ]); + $notice = Notice::singleton(); + $notice->update([ + 'title' => 'Important Notice', + 'is_active' => true, + ]); + + expect(Cache::has('home_page_featured_blogs'))->toBeFalse() + ->and(Cache::has('home_page_notice'))->toBeFalse(); + + $this->get('/')->assertStatus(200); + + expect(Cache::has('home_page_featured_blogs'))->toBeTrue() + ->and(Cache::has('home_page_notice'))->toBeTrue(); + + // Cache should be invalidated when notice is updated + $notice->update(['title' => 'Updated Notice']); + expect(Cache::has('home_page_notice'))->toBeFalse() + ->and(Cache::has('home_page_featured_blogs'))->toBeFalse(); + + // Re-cache and verify blog change invalidates cache + $this->get('/')->assertStatus(200); + expect(Cache::has('home_page_featured_blogs'))->toBeTrue(); + + $blog->update(['title' => 'Updated Blog Title']); + expect(Cache::has('home_page_featured_blogs'))->toBeFalse(); +}); + test('the about us page loads successfully', function () { $response = $this->get('/about-us');