From d43e6d8d0726413f745ca3d2938e7394210c3a9a Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Tue, 6 Jul 2021 20:10:42 -0600 Subject: [PATCH] Update NotificationService, use zrevrangebyscore for api --- app/Http/Controllers/Api/ApiV1Controller.php | 34 +++++------ app/Services/NotificationService.php | 61 +++++++++++++++++++- 2 files changed, 71 insertions(+), 24 deletions(-) diff --git a/app/Http/Controllers/Api/ApiV1Controller.php b/app/Http/Controllers/Api/ApiV1Controller.php index c8bf8c702..e651bb204 100644 --- a/app/Http/Controllers/Api/ApiV1Controller.php +++ b/app/Http/Controllers/Api/ApiV1Controller.php @@ -1283,7 +1283,6 @@ class ApiV1Controller extends Controller $pid = $request->user()->profile_id; $limit = $request->input('limit', 20); - $timeago = now()->subMonths(6); $since = $request->input('since_id'); $min = $request->input('min_id'); @@ -1293,27 +1292,20 @@ class ApiV1Controller extends Controller $min = 1; } - $dir = $since ? '>' : ($min ? '>=' : '<'); - $id = $since ?? $min ?? $max; + $maxId = null; + $minId = null; - $notifications = Notification::whereProfileId($pid) - ->where('id', $dir, $id) - ->whereDate('created_at', '>', $timeago) - ->orderByDesc('id') - ->limit($limit) - ->get(); - - $minId = $notifications->min('id'); - $maxId = $notifications->max('id'); - - $resource = new Fractal\Resource\Collection( - $notifications, - new NotificationTransformer() - ); - - $res = $this->fractal - ->createData($resource) - ->toArray(); + if($max) { + $res = NotificationService::getMax($pid, $max, $limit); + $ids = NotificationService::getRankedMaxId($pid, $max, $limit); + $maxId = max($ids); + $minId = min($ids); + } else { + $res = NotificationService::getMin($pid, $min ?? $since, $limit); + $ids = NotificationService::getRankedMinId($pid, $min ?? $since, $limit); + $maxId = max($ids); + $minId = min($ids); + } $baseUrl = config('app.url') . '/api/v1/notifications?'; diff --git a/app/Services/NotificationService.php b/app/Services/NotificationService.php index bbf7a34f3..3de1bf650 100644 --- a/app/Services/NotificationService.php +++ b/app/Services/NotificationService.php @@ -46,6 +46,60 @@ class NotificationService { return $ids; } + public static function getMax($id = false, $start, $limit = 10) + { + $ids = self::getRankedMaxId($id, $start, $limit); + + if(empty($ids)) { + return []; + } + + $res = collect([]); + foreach($ids as $id) { + $res->push(self::getNotification($id)); + } + return $res->toArray(); + } + + public static function getMin($id = false, $start, $limit = 10) + { + $ids = self::getRankedMinId($id, $start, $limit); + + if(empty($ids)) { + return []; + } + + $res = collect([]); + foreach($ids as $id) { + $res->push(self::getNotification($id)); + } + return $res->toArray(); + } + + public static function getRankedMaxId($id = false, $start = null, $limit = 10) + { + if(!$start || !$id) { + return []; + } + + return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY.$id, $start, '-inf', [ + 'withscores' => true, + 'limit' => [1, $limit] + ])); + } + + public static function getRankedMinId($id = false, $end = null, $limit = 10) + { + if(!$end || !$id) { + return []; + } + + return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY.$id, '+inf', $end, [ + 'withscores' => true, + 'limit' => [0, $limit] + ])); + } + public static function set($id, $val) { return Redis::zadd(self::CACHE_KEY . $id, $val, $val); @@ -53,6 +107,7 @@ class NotificationService { public static function del($id, $val) { + Cache::forget('service:notification:' . $val); return Redis::zrem(self::CACHE_KEY . $id, $val); } @@ -73,7 +128,7 @@ class NotificationService { public static function getNotification($id) { - return Cache::remember('service:notification:'.$id, now()->addMonths(3), function() use($id) { + return Cache::remember('service:notification:'.$id, now()->addDays(3), function() use($id) { $n = Notification::with('item')->findOrFail($id); $fractal = new Fractal\Manager(); $fractal->setSerializer(new ArraySerializer()); @@ -84,7 +139,7 @@ class NotificationService { public static function setNotification(Notification $notification) { - return Cache::remember('service:notification:'.$notification->id, now()->addMonths(3), function() use($notification) { + return Cache::remember('service:notification:'.$notification->id, now()->addDays(3), function() use($notification) { $fractal = new Fractal\Manager(); $fractal->setSerializer(new ArraySerializer()); $resource = new Fractal\Resource\Item($notification, new NotificationTransformer()); @@ -106,4 +161,4 @@ class NotificationService { } return 0; } -} \ No newline at end of file +}