Skip to content

Commit 5e579f5

Browse files
committed
new notification count caching
1 parent 1451c7c commit 5e579f5

File tree

5 files changed

+22
-8
lines changed

5 files changed

+22
-8
lines changed

framework/core/src/Api/Controller/ListNotificationsController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ protected function data(ServerRequestInterface $request, Document $document)
6464

6565
$actor->markNotificationsAsRead()->save();
6666

67+
// Invalidate new notification count cache since read_notifications_at changed
68+
resolve('cache.store')->forget("user.{$actor->id}.new_notification_count");
69+
6770
$limit = $this->extractLimit($request);
6871
$offset = $this->extractOffset($request);
6972
$include = $this->extractInclude($request);

framework/core/src/Notification/Command/ReadAllNotificationsHandler.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ public function handle(ReadAllNotifications $command)
4848

4949
$this->notifications->markAllAsRead($actor);
5050

51-
// Invalidate notification count cache
52-
resolve('cache.store')->forget("user.{$actor->id}.unread_notification_count");
51+
// Invalidate notification count caches
52+
$cache = resolve('cache.store');
53+
$cache->forget("user.{$actor->id}.unread_notification_count");
54+
$cache->forget("user.{$actor->id}.new_notification_count");
5355

5456
$this->events->dispatch(new ReadAll($actor, Carbon::now()));
5557
}

framework/core/src/Notification/Command/ReadNotificationHandler.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ public function handle(ReadNotification $command)
5151

5252
$notification->read_at = Carbon::now();
5353

54-
// Invalidate notification count cache
55-
resolve('cache.store')->forget("user.{$actor->id}.unread_notification_count");
54+
// Invalidate notification count caches
55+
$cache = resolve('cache.store');
56+
$cache->forget("user.{$actor->id}.unread_notification_count");
57+
$cache->forget("user.{$actor->id}.new_notification_count");
5658

5759
$this->events->dispatch(new Read($actor, $notification, Carbon::now()));
5860

framework/core/src/Notification/Notification.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,11 @@ public static function notify(array $recipients, BlueprintInterface $blueprint)
234234
}, $recipients)
235235
);
236236

237-
// Invalidate notification count cache for all recipients
237+
// Invalidate notification count caches for all recipients
238238
$cache = resolve('cache.store');
239239
foreach ($recipients as $user) {
240240
$cache->forget("user.{$user->id}.unread_notification_count");
241+
$cache->forget("user.{$user->id}.new_notification_count");
241242
}
242243
}
243244

framework/core/src/User/User.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,15 @@ protected function getUnreadNotifications()
472472
*/
473473
public function getNewNotificationCount()
474474
{
475-
return $this->unreadNotifications()
476-
->where('created_at', '>', $this->read_notifications_at ?? 0)
477-
->count();
475+
// Cache notification count for 5 minutes to avoid expensive query on every page load
476+
// Cache is actively invalidated when notifications change or user views notifications
477+
$cacheKey = "user.{$this->id}.new_notification_count";
478+
479+
return resolve('cache.store')->remember($cacheKey, 300, function () {
480+
return $this->unreadNotifications()
481+
->where('created_at', '>', $this->read_notifications_at ?? 0)
482+
->count();
483+
});
478484
}
479485

480486
/**

0 commit comments

Comments
 (0)