From 2496386d9b0403a47835cb8ac2fd896f0a3a7b0f Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sun, 17 Sep 2023 23:51:42 -0600 Subject: [PATCH 1/3] Update NotificationService, improve cache warming query --- app/Services/NotificationService.php | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/app/Services/NotificationService.php b/app/Services/NotificationService.php index 139b13a69..81ac0b912 100644 --- a/app/Services/NotificationService.php +++ b/app/Services/NotificationService.php @@ -16,6 +16,8 @@ use League\Fractal\Pagination\IlluminatePaginatorAdapter; class NotificationService { const CACHE_KEY = 'pf:services:notifications:ids:'; + const EPOCH_CACHE_KEY = 'pf:services:notifications:epoch-id:by-months:'; + const ITEM_CACHE_TTL = 86400; const MASTODON_TYPES = [ 'follow', 'follow_request', @@ -44,11 +46,18 @@ class NotificationService { return $res; } + public static function getEpochId($months = 6) + { + return Cache::remember(self::EPOCH_CACHE_KEY . $months, 1209600, function() use($months) { + return Notification::where('created_at', '>', now()->subMonths($months))->first()->id; + }); + } + public static function coldGet($id, $start = 0, $stop = 400) { $stop = $stop > 400 ? 400 : $stop; - $ids = Notification::whereProfileId($id) - ->latest() + $ids = Notification::where('id', '>', self::getEpochId()) + ->where('profile_id', $id) ->skip($start) ->take($stop) ->pluck('id'); @@ -227,7 +236,7 @@ class NotificationService { public static function getNotification($id) { - $notification = Cache::remember('service:notification:'.$id, 86400, function() use($id) { + $notification = Cache::remember('service:notification:'.$id, self::ITEM_CACHE_TTL, function() use($id) { $n = Notification::with('item')->find($id); if(!$n) { @@ -259,19 +268,19 @@ class NotificationService { public static function setNotification(Notification $notification) { - return Cache::remember('service:notification:'.$notification->id, now()->addDays(3), function() use($notification) { + return Cache::remember('service:notification:'.$notification->id, self::ITEM_CACHE_TTL, function() use($notification) { $fractal = new Fractal\Manager(); $fractal->setSerializer(new ArraySerializer()); $resource = new Fractal\Resource\Item($notification, new NotificationTransformer()); return $fractal->createData($resource)->toArray(); }); - } + } public static function warmCache($id, $stop = 400, $force = false) { if(self::count($id) == 0 || $force == true) { - $ids = Notification::whereProfileId($id) - ->latest() + $ids = Notification::where('profile_id', $id) + ->where('id', '>', self::getEpochId()) ->limit($stop) ->pluck('id'); foreach($ids as $key) { From 223661ecb2555e068346a47a989d105cbbd0d782 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sun, 17 Sep 2023 23:54:31 -0600 Subject: [PATCH 2/3] Update StatusService, hydrate accounts on request instead of caching them along with status objects --- app/Services/StatusService.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/app/Services/StatusService.php b/app/Services/StatusService.php index bdf2f31db..9730bb2d4 100644 --- a/app/Services/StatusService.php +++ b/app/Services/StatusService.php @@ -22,9 +22,9 @@ class StatusService return self::CACHE_KEY . $p . $id; } - public static function get($id, $publicOnly = true) + public static function get($id, $publicOnly = true, $mastodonMode = false) { - return Cache::remember(self::key($id, $publicOnly), 21600, function() use($id, $publicOnly) { + $res = Cache::remember(self::key($id, $publicOnly), 21600, function() use($id, $publicOnly) { if($publicOnly) { $status = Status::whereScope('public')->find($id); } else { @@ -36,13 +36,23 @@ class StatusService $fractal = new Fractal\Manager(); $fractal->setSerializer(new ArraySerializer()); $resource = new Fractal\Resource\Item($status, new StatusStatelessTransformer()); - return $fractal->createData($resource)->toArray(); + $res = $fractal->createData($resource)->toArray(); + $res['_pid'] = isset($res['account']) && isset($res['account']['id']) ? $res['account']['id'] : null; + if(isset($res['_pid'])) { + unset($res['account']); + } + return $res; }); + if($res) { + $res['account'] = $mastodonMode === true ? AccountService::getMastodon($res['_pid'], true) : AccountService::get($res['_pid'], true); + unset($res['_pid']); + } + return $res; } public static function getMastodon($id, $publicOnly = true) { - $status = self::get($id, $publicOnly); + $status = self::get($id, $publicOnly, true); if(!$status) { return null; } From 5ab7f9958ccaebb1e7cd1b380a1fd48c7718252d Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sun, 17 Sep 2023 23:57:52 -0600 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45f5a80de..024aa7423 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ - Update FollowerService, add forget method to RelationshipService call to reduce load when mass purging ([347e4f59](https://github.com/pixelfed/pixelfed/commit/347e4f59)) - Update FollowServiceWarmCache, improve handling larger following/follower lists ([61a6d904](https://github.com/pixelfed/pixelfed/commit/61a6d904)) - Update StoryApiV1Controller, add viewers route to view story viewers ([941736ce](https://github.com/pixelfed/pixelfed/commit/941736ce)) +- Update NotificationService, improve cache warming query ([2496386d](https://github.com/pixelfed/pixelfed/commit/2496386d)) +- Update StatusService, hydrate accounts on request instead of caching them along with status objects ([223661ec](https://github.com/pixelfed/pixelfed/commit/223661ec)) - ([](https://github.com/pixelfed/pixelfed/commit/)) ## [v0.11.9 (2023-08-21)](https://github.com/pixelfed/pixelfed/compare/v0.11.8...v0.11.9)