1
0
Fork 0
forked from mirror/pixelfed
pixelfed/app/Services/NotificationService.php

165 lines
3.7 KiB
PHP
Raw Normal View History

2019-05-01 04:40:15 +00:00
<?php
namespace App\Services;
2019-12-11 06:04:03 +00:00
use Cache;
use Illuminate\Support\Facades\Redis;
2019-05-01 04:40:15 +00:00
use App\{
Notification,
Profile
};
use App\Transformer\Api\NotificationTransformer;
use League\Fractal;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
class NotificationService {
const CACHE_KEY = 'pf:services:notifications:ids:';
2019-06-05 07:18:09 +00:00
public static function get($id, $start = 0, $stop = 400)
2019-05-01 04:40:15 +00:00
{
$res = collect([]);
$key = self::CACHE_KEY . $id;
2019-06-05 07:18:09 +00:00
$stop = $stop > 400 ? 400 : $stop;
2019-05-01 04:40:15 +00:00
$ids = Redis::zrangebyscore($key, $start, $stop);
if(empty($ids)) {
$ids = self::coldGet($id, $start, $stop);
}
foreach($ids as $id) {
$res->push(self::getNotification($id));
}
return $res;
}
2019-06-05 07:18:09 +00:00
public static function coldGet($id, $start = 0, $stop = 400)
2019-05-01 04:40:15 +00:00
{
2019-06-05 07:18:09 +00:00
$stop = $stop > 400 ? 400 : $stop;
2019-05-01 04:40:15 +00:00
$ids = Notification::whereProfileId($id)
->latest()
->skip($start)
->take($stop)
->pluck('id');
foreach($ids as $key) {
self::set($id, $key);
}
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]
]));
}
2019-05-01 04:40:15 +00:00
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);
2019-05-01 04:40:15 +00:00
return Redis::zrem(self::CACHE_KEY . $id, $val);
}
public static function add($id, $val)
{
return self::set($id, $val);
}
public static function rem($id, $val)
{
return self::del($id, $val);
}
public static function count($id)
{
return Redis::zcount(self::CACHE_KEY . $id, '-inf', '+inf');
}
public static function getNotification($id)
{
return Cache::remember('service:notification:'.$id, now()->addDays(3), function() use($id) {
2019-05-27 05:05:29 +00:00
$n = Notification::with('item')->findOrFail($id);
2019-05-01 04:40:15 +00:00
$fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Item($n, new NotificationTransformer());
return $fractal->createData($resource)->toArray();
});
2019-05-21 03:32:35 +00:00
}
public static function setNotification(Notification $notification)
{
return Cache::remember('service:notification:'.$notification->id, now()->addDays(3), function() use($notification) {
2019-05-21 03:32:35 +00:00
$fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Item($notification, new NotificationTransformer());
return $fractal->createData($resource)->toArray();
});
2019-05-01 04:40:15 +00:00
}
2019-06-05 07:18:09 +00:00
public static function warmCache($id, $stop = 400, $force = false)
2019-05-01 04:40:15 +00:00
{
if(self::count($id) == 0 || $force == true) {
$ids = Notification::whereProfileId($id)
->latest()
->limit($stop)
->pluck('id');
foreach($ids as $key) {
self::set($id, $key);
}
return 1;
}
return 0;
}
}