Update FollowerService, use redis sorted sets for follower relations

This commit is contained in:
Daniel Supernault 2022-12-05 05:27:27 -07:00
parent 3ef66628ad
commit 356cc2774a
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
4 changed files with 58 additions and 5 deletions

View file

@ -11,6 +11,7 @@ use DB;
use Storage; use Storage;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use App\Services\AccountService; use App\Services\AccountService;
use App\Services\FollowerService;
use App\Services\PublicTimelineService; use App\Services\PublicTimelineService;
use App\{ use App\{
AccountInterstitial, AccountInterstitial,
@ -133,7 +134,10 @@ class DeleteAccountPipeline implements ShouldQueue
->forceDelete(); ->forceDelete();
Follower::whereProfileId($id) Follower::whereProfileId($id)
->orWhere('following_id', $id) ->orWhere('following_id', $id)
->forceDelete(); ->each(function($follow) {
FollowerService::remove($follow->profile_id, $follow->following_id);
$follow->delete();
});
Like::whereProfileId($id)->forceDelete(); Like::whereProfileId($id)->forceDelete();
}); });

View file

@ -9,6 +9,7 @@ use App\User;
use App\UserSetting; use App\UserSetting;
use App\Jobs\FollowPipeline\FollowPipeline; use App\Jobs\FollowPipeline\FollowPipeline;
use DB; use DB;
use App\Services\FollowerService;
class UserObserver 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);
}
} }

View file

@ -14,6 +14,8 @@ use App\{
class FollowerService class FollowerService
{ {
const CACHE_KEY = 'pf:services:followers:'; 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 FOLLOWING_KEY = 'pf:services:follow:following:id:';
const FOLLOWERS_KEY = 'pf:services:follow:followers:id:'; const FOLLOWERS_KEY = 'pf:services:follow:followers:id:';
@ -35,17 +37,45 @@ class FollowerService
public static function followers($id, $start = 0, $stop = 10) public static function followers($id, $start = 0, $stop = 10)
{ {
self::cacheSyncCheck($id, 'followers');
return Redis::zrange(self::FOLLOWERS_KEY . $id, $start, $stop); return Redis::zrange(self::FOLLOWERS_KEY . $id, $start, $stop);
} }
public static function following($id, $start = 0, $stop = 10) public static function following($id, $start = 0, $stop = 10)
{ {
self::cacheSyncCheck($id, 'following');
return Redis::zrange(self::FOLLOWING_KEY . $id, $start, $stop); return Redis::zrange(self::FOLLOWING_KEY . $id, $start, $stop);
} }
public static function follows(string $actor, string $target) 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) 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);
}
} }

View file

@ -14,8 +14,8 @@ class RelationshipService
public static function get($aid, $tid) public static function get($aid, $tid)
{ {
$actor = AccountService::get($aid); $actor = AccountService::get($aid, true);
$target = AccountService::get($tid); $target = AccountService::get($tid, true);
if(!$actor || !$target) { if(!$actor || !$target) {
return self::defaultRelation($tid); return self::defaultRelation($tid);
} }