mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-25 15:55:22 +00:00
Update FollowerService, use redis sorted sets for follower relations
This commit is contained in:
parent
3ef66628ad
commit
356cc2774a
4 changed files with 58 additions and 5 deletions
|
@ -11,6 +11,7 @@ use DB;
|
|||
use Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use App\Services\AccountService;
|
||||
use App\Services\FollowerService;
|
||||
use App\Services\PublicTimelineService;
|
||||
use App\{
|
||||
AccountInterstitial,
|
||||
|
@ -133,7 +134,10 @@ class DeleteAccountPipeline implements ShouldQueue
|
|||
->forceDelete();
|
||||
Follower::whereProfileId($id)
|
||||
->orWhere('following_id', $id)
|
||||
->forceDelete();
|
||||
->each(function($follow) {
|
||||
FollowerService::remove($follow->profile_id, $follow->following_id);
|
||||
$follow->delete();
|
||||
});
|
||||
Like::whereProfileId($id)->forceDelete();
|
||||
});
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ use App\User;
|
|||
use App\UserSetting;
|
||||
use App\Jobs\FollowPipeline\FollowPipeline;
|
||||
use DB;
|
||||
use App\Services\FollowerService;
|
||||
|
||||
class UserObserver
|
||||
{
|
||||
|
@ -85,6 +86,16 @@ class UserObserver
|
|||
]);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the user "deleted" event.
|
||||
*
|
||||
* @param \App\User $user
|
||||
* @return void
|
||||
*/
|
||||
public function deleted(User $user)
|
||||
{
|
||||
FollowerService::delCache($user->profile_id);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,6 +14,8 @@ use App\{
|
|||
class FollowerService
|
||||
{
|
||||
const CACHE_KEY = 'pf:services:followers:';
|
||||
const FOLLOWERS_SYNC_KEY = 'pf:services:followers:sync-followers:';
|
||||
const FOLLOWING_SYNC_KEY = 'pf:services:followers:sync-following:';
|
||||
const FOLLOWING_KEY = 'pf:services:follow:following:id:';
|
||||
const FOLLOWERS_KEY = 'pf:services:follow:followers:id:';
|
||||
|
||||
|
@ -35,17 +37,45 @@ class FollowerService
|
|||
|
||||
public static function followers($id, $start = 0, $stop = 10)
|
||||
{
|
||||
self::cacheSyncCheck($id, 'followers');
|
||||
return Redis::zrange(self::FOLLOWERS_KEY . $id, $start, $stop);
|
||||
}
|
||||
|
||||
public static function following($id, $start = 0, $stop = 10)
|
||||
{
|
||||
self::cacheSyncCheck($id, 'following');
|
||||
return Redis::zrange(self::FOLLOWING_KEY . $id, $start, $stop);
|
||||
}
|
||||
|
||||
public static function follows(string $actor, string $target)
|
||||
{
|
||||
return Follower::whereProfileId($actor)->whereFollowingId($target)->exists();
|
||||
self::cacheSyncCheck($target, 'followers');
|
||||
return (bool) Redis::zScore(self::FOLLOWERS_KEY . $target, $actor);
|
||||
}
|
||||
|
||||
public static function cacheSyncCheck($id, $scope = 'followers')
|
||||
{
|
||||
if($scope === 'followers') {
|
||||
if(Cache::get(self::FOLLOWERS_SYNC_KEY . $id) != null) {
|
||||
return;
|
||||
}
|
||||
$followers = Follower::whereFollowingId($id)->pluck('profile_id');
|
||||
$followers->each(function($fid) use($id) {
|
||||
self::add($fid, $id);
|
||||
});
|
||||
Cache::put(self::FOLLOWERS_SYNC_KEY . $id, 1, 604800);
|
||||
}
|
||||
if($scope === 'following') {
|
||||
if(Cache::get(self::FOLLOWING_SYNC_KEY . $id) != null) {
|
||||
return;
|
||||
}
|
||||
$followers = Follower::whereProfileId($id)->pluck('following_id');
|
||||
$followers->each(function($fid) use($id) {
|
||||
self::add($id, $fid);
|
||||
});
|
||||
Cache::put(self::FOLLOWING_SYNC_KEY . $id, 1, 604800);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
public static function audience($profile, $scope = null)
|
||||
|
@ -114,4 +144,12 @@ class FollowerService
|
|||
});
|
||||
}
|
||||
|
||||
public static function delCache($id)
|
||||
{
|
||||
Redis::del(self::CACHE_KEY . $id);
|
||||
Redis::del(self::FOLLOWING_KEY . $id);
|
||||
Redis::del(self::FOLLOWERS_KEY . $id);
|
||||
Redis::del(self::FOLLOWERS_SYNC_KEY . $id);
|
||||
Redis::del(self::FOLLOWING_SYNC_KEY . $id);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,8 +14,8 @@ class RelationshipService
|
|||
|
||||
public static function get($aid, $tid)
|
||||
{
|
||||
$actor = AccountService::get($aid);
|
||||
$target = AccountService::get($tid);
|
||||
$actor = AccountService::get($aid, true);
|
||||
$target = AccountService::get($tid, true);
|
||||
if(!$actor || !$target) {
|
||||
return self::defaultRelation($tid);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue