Update Profile model, improve counter caching

This commit is contained in:
Daniel Supernault 2020-12-10 22:58:13 -07:00
parent b4573a8f0a
commit 4a14e970f0
No known key found for this signature in database
GPG Key ID: 0DEF1C662C9033F7
5 changed files with 62 additions and 23 deletions

View File

@ -376,6 +376,9 @@ class AccountController extends Controller
break;
}
Cache::forget('profile:follower_count:'.$pid);
Cache::forget('profile:following_count:'.$pid);
return response()->json(['msg' => 'success'], 200);
}

View File

@ -468,6 +468,10 @@ class ApiV1Controller extends Controller
Cache::forget('api:local:exp:rec:'.$user->profile_id);
Cache::forget('user:account:id:'.$target->user_id);
Cache::forget('user:account:id:'.$user->id);
Cache::forget('profile:follower_count:'.$target->id);
Cache::forget('profile:follower_count:'.$user->profile_id);
Cache::forget('profile:following_count:'.$target->id);
Cache::forget('profile:following_count:'.$user->profile_id);
$resource = new Fractal\Resource\Item($target, new RelationshipTransformer());
$res = $this->fractal->createData($resource)->toArray();

View File

@ -115,6 +115,10 @@ class FollowerController extends Controller
Cache::forget('px:profile:followers-v1.3:'.$target->id);
Cache::forget('px:profile:following-v1.3:'.$user->id);
Cache::forget('px:profile:following-v1.3:'.$target->id);
Cache::forget('profile:follower_count:'.$target->id);
Cache::forget('profile:follower_count:'.$user->id);
Cache::forget('profile:following_count:'.$target->id);
Cache::forget('profile:following_count:'.$user->id);
return $target->url();
}

View File

@ -64,22 +64,52 @@ class Profile extends Model
public function followingCount($short = false)
{
$count = $this->following()->count();
if ($short) {
return PrettyNumber::convert($count);
} else {
$count = Cache::remember('profile:following_count:'.$this->id, now()->addMonths(1), function() {
$count = $this->following_count;
if($count) {
return $count;
}
$count = $this->following()->count();
$this->following_count = $count;
$this->save();
return $count;
}
});
return $short ? PrettyNumber::convert($count) : $count;
}
public function followerCount($short = false)
{
$count = $this->followers()->count();
if ($short) {
return PrettyNumber::convert($count);
} else {
$count = Cache::remember('profile:follower_count:'.$this->id, now()->addMonths(1), function() {
$count = $this->followers_count;
if($count) {
return $count;
}
$count = $this->followers()->count();
$this->followers_count = $count;
$this->save();
return $count;
}
});
return $short ? PrettyNumber::convert($count) : $count;
}
public function statusCount()
{
return Cache::remember('profile:status_count:'.$this->id, now()->addMonths(1), function() {
$count = $this->status_count;
if($count) {
return $count;
}
$count = $this->statuses()
->getQuery()
->whereHas('media')
->whereNull('in_reply_to_id')
->whereNull('reblog_of_id')
->count();
$this->status_count = $count;
$this->save();
return $count;
});
}
public function following()
@ -148,18 +178,6 @@ class Profile extends Model
return $url;
}
public function statusCount()
{
return Cache::remember('profile:status_count:'.$this->id, now()->addMonths(1), function() {
return $this->statuses()
->getQuery()
->whereHas('media')
->whereNull('in_reply_to_id')
->whereNull('reblog_of_id')
->count();
});
}
// deprecated
public function recommendFollowers()
{

View File

@ -145,7 +145,7 @@ class Inbox
return;
}
$to = $activity['to'];
$cc = $activity['cc'];
$cc = isset($activity['cc']) ? $activity['cc'] : [];
if(count($to) == 1 &&
count($cc) == 0 &&
parse_url($to[0], PHP_URL_HOST) == config('pixelfed.domain.app')
@ -342,6 +342,12 @@ class Inbox
'follower_id' => $actor->id,
'following_id' => $target->id
]);
Cache::forget('profile:follower_count:'.$target->id);
Cache::forget('profile:follower_count:'.$actor->id);
Cache::forget('profile:following_count:'.$target->id);
Cache::forget('profile:following_count:'.$actor->id);
} else {
$follower = new Follower;
$follower->profile_id = $actor->id;
@ -365,6 +371,10 @@ class Inbox
]
];
Helpers::sendSignedObject($target, $actor->inbox_url, $accept);
Cache::forget('profile:follower_count:'.$target->id);
Cache::forget('profile:follower_count:'.$actor->id);
Cache::forget('profile:following_count:'.$target->id);
Cache::forget('profile:following_count:'.$actor->id);
}
}