Skip to content

Commit

Permalink
Update NotificationService, use zrevrangebyscore for api
Browse files Browse the repository at this point in the history
  • Loading branch information
dansup committed Jul 7, 2021
1 parent 76b6ec2 commit d43e6d8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 24 deletions.
34 changes: 13 additions & 21 deletions app/Http/Controllers/Api/ApiV1Controller.php
Expand Up @@ -1283,7 +1283,6 @@ public function accountNotifications(Request $request)

$pid = $request->user()->profile_id;
$limit = $request->input('limit', 20);
$timeago = now()->subMonths(6);

$since = $request->input('since_id');
$min = $request->input('min_id');
Expand All @@ -1293,27 +1292,20 @@ public function accountNotifications(Request $request)
$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?';

Expand Down
61 changes: 58 additions & 3 deletions app/Services/NotificationService.php
Expand Up @@ -46,13 +46,68 @@ public static function coldGet($id, $start = 0, $stop = 400)
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);
}

public static function del($id, $val)
{
Cache::forget('service:notification:' . $val);
return Redis::zrem(self::CACHE_KEY . $id, $val);
}

Expand All @@ -73,7 +128,7 @@ public static function count($id)

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());
Expand All @@ -84,7 +139,7 @@ public static function getNotification($id)

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());
Expand All @@ -106,4 +161,4 @@ public static function warmCache($id, $stop = 400, $force = false)
}
return 0;
}
}
}

0 comments on commit d43e6d8

Please sign in to comment.