Update NotificationService, improve cache warming query

pull/4654/head
Daniel Supernault 1 year ago
parent 33ed7a8c91
commit 2496386d9b
No known key found for this signature in database
GPG Key ID: 0DEF1C662C9033F7

@ -16,6 +16,8 @@ use League\Fractal\Pagination\IlluminatePaginatorAdapter;
class NotificationService { class NotificationService {
const CACHE_KEY = 'pf:services:notifications:ids:'; 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 = [ const MASTODON_TYPES = [
'follow', 'follow',
'follow_request', 'follow_request',
@ -44,11 +46,18 @@ class NotificationService {
return $res; 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) public static function coldGet($id, $start = 0, $stop = 400)
{ {
$stop = $stop > 400 ? 400 : $stop; $stop = $stop > 400 ? 400 : $stop;
$ids = Notification::whereProfileId($id) $ids = Notification::where('id', '>', self::getEpochId())
->latest() ->where('profile_id', $id)
->skip($start) ->skip($start)
->take($stop) ->take($stop)
->pluck('id'); ->pluck('id');
@ -227,7 +236,7 @@ class NotificationService {
public static function getNotification($id) 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); $n = Notification::with('item')->find($id);
if(!$n) { if(!$n) {
@ -259,19 +268,19 @@ class NotificationService {
public static function setNotification(Notification $notification) 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 = 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());
return $fractal->createData($resource)->toArray(); return $fractal->createData($resource)->toArray();
}); });
} }
public static function warmCache($id, $stop = 400, $force = false) public static function warmCache($id, $stop = 400, $force = false)
{ {
if(self::count($id) == 0 || $force == true) { if(self::count($id) == 0 || $force == true) {
$ids = Notification::whereProfileId($id) $ids = Notification::where('profile_id', $id)
->latest() ->where('id', '>', self::getEpochId())
->limit($stop) ->limit($stop)
->pluck('id'); ->pluck('id');
foreach($ids as $key) { foreach($ids as $key) {

Loading…
Cancel
Save