Update NotificationService, use zrevrangebyscore for api

This commit is contained in:
Daniel Supernault 2021-07-06 20:10:42 -06:00
parent 76b6ec2aa6
commit d43e6d8d07
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
2 changed files with 71 additions and 24 deletions

View file

@ -1283,7 +1283,6 @@ class ApiV1Controller extends Controller
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$limit = $request->input('limit', 20); $limit = $request->input('limit', 20);
$timeago = now()->subMonths(6);
$since = $request->input('since_id'); $since = $request->input('since_id');
$min = $request->input('min_id'); $min = $request->input('min_id');
@ -1293,27 +1292,20 @@ class ApiV1Controller extends Controller
$min = 1; $min = 1;
} }
$dir = $since ? '>' : ($min ? '>=' : '<'); $maxId = null;
$id = $since ?? $min ?? $max; $minId = null;
$notifications = Notification::whereProfileId($pid) if($max) {
->where('id', $dir, $id) $res = NotificationService::getMax($pid, $max, $limit);
->whereDate('created_at', '>', $timeago) $ids = NotificationService::getRankedMaxId($pid, $max, $limit);
->orderByDesc('id') $maxId = max($ids);
->limit($limit) $minId = min($ids);
->get(); } else {
$res = NotificationService::getMin($pid, $min ?? $since, $limit);
$minId = $notifications->min('id'); $ids = NotificationService::getRankedMinId($pid, $min ?? $since, $limit);
$maxId = $notifications->max('id'); $maxId = max($ids);
$minId = min($ids);
$resource = new Fractal\Resource\Collection( }
$notifications,
new NotificationTransformer()
);
$res = $this->fractal
->createData($resource)
->toArray();
$baseUrl = config('app.url') . '/api/v1/notifications?'; $baseUrl = config('app.url') . '/api/v1/notifications?';

View file

@ -46,6 +46,60 @@ class NotificationService {
return $ids; 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) public static function set($id, $val)
{ {
return Redis::zadd(self::CACHE_KEY . $id, $val, $val); return Redis::zadd(self::CACHE_KEY . $id, $val, $val);
@ -53,6 +107,7 @@ class NotificationService {
public static function del($id, $val) public static function del($id, $val)
{ {
Cache::forget('service:notification:' . $val);
return Redis::zrem(self::CACHE_KEY . $id, $val); return Redis::zrem(self::CACHE_KEY . $id, $val);
} }
@ -73,7 +128,7 @@ class NotificationService {
public static function getNotification($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); $n = Notification::with('item')->findOrFail($id);
$fractal = new Fractal\Manager(); $fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer()); $fractal->setSerializer(new ArraySerializer());
@ -84,7 +139,7 @@ class NotificationService {
public static function setNotification(Notification $notification) 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 = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer()); $fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Item($notification, new NotificationTransformer()); $resource = new Fractal\Resource\Item($notification, new NotificationTransformer());
@ -106,4 +161,4 @@ class NotificationService {
} }
return 0; return 0;
} }
} }