mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-12-25 17:26:43 +00:00
Merge pull request #4654 from pixelfed/staging
Update NotificationService, improve cache warming query
This commit is contained in:
commit
4112ab5f83
3 changed files with 32 additions and 11 deletions
|
@ -11,6 +11,8 @@
|
||||||
- Update FollowerService, add forget method to RelationshipService call to reduce load when mass purging ([347e4f59](https://github.com/pixelfed/pixelfed/commit/347e4f59))
|
- Update FollowerService, add forget method to RelationshipService call to reduce load when mass purging ([347e4f59](https://github.com/pixelfed/pixelfed/commit/347e4f59))
|
||||||
- Update FollowServiceWarmCache, improve handling larger following/follower lists ([61a6d904](https://github.com/pixelfed/pixelfed/commit/61a6d904))
|
- Update FollowServiceWarmCache, improve handling larger following/follower lists ([61a6d904](https://github.com/pixelfed/pixelfed/commit/61a6d904))
|
||||||
- Update StoryApiV1Controller, add viewers route to view story viewers ([941736ce](https://github.com/pixelfed/pixelfed/commit/941736ce))
|
- Update StoryApiV1Controller, add viewers route to view story viewers ([941736ce](https://github.com/pixelfed/pixelfed/commit/941736ce))
|
||||||
|
- Update NotificationService, improve cache warming query ([2496386d](https://github.com/pixelfed/pixelfed/commit/2496386d))
|
||||||
|
- Update StatusService, hydrate accounts on request instead of caching them along with status objects ([223661ec](https://github.com/pixelfed/pixelfed/commit/223661ec))
|
||||||
- ([](https://github.com/pixelfed/pixelfed/commit/))
|
- ([](https://github.com/pixelfed/pixelfed/commit/))
|
||||||
|
|
||||||
## [v0.11.9 (2023-08-21)](https://github.com/pixelfed/pixelfed/compare/v0.11.8...v0.11.9)
|
## [v0.11.9 (2023-08-21)](https://github.com/pixelfed/pixelfed/compare/v0.11.8...v0.11.9)
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
|
@ -22,9 +22,9 @@ class StatusService
|
||||||
return self::CACHE_KEY . $p . $id;
|
return self::CACHE_KEY . $p . $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function get($id, $publicOnly = true)
|
public static function get($id, $publicOnly = true, $mastodonMode = false)
|
||||||
{
|
{
|
||||||
return Cache::remember(self::key($id, $publicOnly), 21600, function() use($id, $publicOnly) {
|
$res = Cache::remember(self::key($id, $publicOnly), 21600, function() use($id, $publicOnly) {
|
||||||
if($publicOnly) {
|
if($publicOnly) {
|
||||||
$status = Status::whereScope('public')->find($id);
|
$status = Status::whereScope('public')->find($id);
|
||||||
} else {
|
} else {
|
||||||
|
@ -36,13 +36,23 @@ class StatusService
|
||||||
$fractal = new Fractal\Manager();
|
$fractal = new Fractal\Manager();
|
||||||
$fractal->setSerializer(new ArraySerializer());
|
$fractal->setSerializer(new ArraySerializer());
|
||||||
$resource = new Fractal\Resource\Item($status, new StatusStatelessTransformer());
|
$resource = new Fractal\Resource\Item($status, new StatusStatelessTransformer());
|
||||||
return $fractal->createData($resource)->toArray();
|
$res = $fractal->createData($resource)->toArray();
|
||||||
|
$res['_pid'] = isset($res['account']) && isset($res['account']['id']) ? $res['account']['id'] : null;
|
||||||
|
if(isset($res['_pid'])) {
|
||||||
|
unset($res['account']);
|
||||||
|
}
|
||||||
|
return $res;
|
||||||
});
|
});
|
||||||
|
if($res) {
|
||||||
|
$res['account'] = $mastodonMode === true ? AccountService::getMastodon($res['_pid'], true) : AccountService::get($res['_pid'], true);
|
||||||
|
unset($res['_pid']);
|
||||||
|
}
|
||||||
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getMastodon($id, $publicOnly = true)
|
public static function getMastodon($id, $publicOnly = true)
|
||||||
{
|
{
|
||||||
$status = self::get($id, $publicOnly);
|
$status = self::get($id, $publicOnly, true);
|
||||||
if(!$status) {
|
if(!$status) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue