ソースを参照

Merge pull request #4654 from pixelfed/staging

Update NotificationService, improve cache warming query
daniel 1 年間 前
コミット
4112ab5f83
3 ファイル変更32 行追加11 行削除
  1. 2 0
      CHANGELOG.md
  2. 16 7
      app/Services/NotificationService.php
  3. 14 4
      app/Services/StatusService.php

+ 2 - 0
CHANGELOG.md

@@ -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 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 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/))
 
 ## [v0.11.9 (2023-08-21)](https://github.com/pixelfed/pixelfed/compare/v0.11.8...v0.11.9)

+ 16 - 7
app/Services/NotificationService.php

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

+ 14 - 4
app/Services/StatusService.php

@@ -22,9 +22,9 @@ class StatusService
         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) {
                 $status = Status::whereScope('public')->find($id);
             } else {
@@ -36,13 +36,23 @@ class StatusService
             $fractal = new Fractal\Manager();
             $fractal->setSerializer(new ArraySerializer());
             $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)
     {
-        $status = self::get($id, $publicOnly);
+        $status = self::get($id, $publicOnly, true);
         if(!$status) {
             return null;
         }