|
@@ -12,8 +12,8 @@ use App\{
|
|
class FollowerService {
|
|
class FollowerService {
|
|
|
|
|
|
protected $profile;
|
|
protected $profile;
|
|
- protected $follower_prefix;
|
|
|
|
- protected $following_prefix;
|
|
|
|
|
|
+ static $follower_prefix = 'px:profile:followers-v1.3:';
|
|
|
|
+ static $following_prefix = 'px:profile:following-v1.3:';
|
|
|
|
|
|
public static function build()
|
|
public static function build()
|
|
{
|
|
{
|
|
@@ -23,35 +23,48 @@ class FollowerService {
|
|
public function profile(Profile $profile)
|
|
public function profile(Profile $profile)
|
|
{
|
|
{
|
|
$this->profile = $profile;
|
|
$this->profile = $profile;
|
|
- $this->follower_prefix = config('cache.prefix').':profile:followers:'.$profile->id;
|
|
|
|
- $this->following_prefix = config('cache.prefix').':profile:following:'.$profile->id;
|
|
|
|
|
|
+ self::$follower_prefix .= $profile->id;
|
|
|
|
+ self::$following_prefix .= $profile->id;
|
|
return $this;
|
|
return $this;
|
|
}
|
|
}
|
|
|
|
|
|
- public function followers($limit = 100, $offset = 0)
|
|
|
|
|
|
+ public function followers($limit = 100, $offset = 1)
|
|
{
|
|
{
|
|
- if(Redis::llen($this->follower_prefix) == 0) {
|
|
|
|
- $followers = $this->profile->followers;
|
|
|
|
|
|
+ if(Redis::zcard(self::$follower_prefix) == 0) {
|
|
|
|
+ $followers = $this->profile->followers()->pluck('profile_id');
|
|
$followers->map(function($i) {
|
|
$followers->map(function($i) {
|
|
- Redis::lpush($this->follower_prefix, $i->id);
|
|
|
|
|
|
+ Redis::zadd(self::$follower_prefix, $i, $i);
|
|
});
|
|
});
|
|
- return $followers;
|
|
|
|
|
|
+ return Redis::zrevrange(self::$follower_prefix, $offset, $limit);
|
|
} else {
|
|
} else {
|
|
- return Redis::lrange($this->follower_prefix, $offset, $limit);
|
|
|
|
|
|
+ return Redis::zrevrange(self::$follower_prefix, $offset, $limit);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- public function following($limit = 100, $offset = 0)
|
|
|
|
|
|
+ public function following($limit = 100, $offset = 1)
|
|
{
|
|
{
|
|
- if(Redis::llen($this->following_prefix) == 0) {
|
|
|
|
- $following = $this->profile->following;
|
|
|
|
|
|
+ if(Redis::zcard(self::$following_prefix) == 0) {
|
|
|
|
+ $following = $this->profile->following()->pluck('following_id');
|
|
$following->map(function($i) {
|
|
$following->map(function($i) {
|
|
- Redis::lpush($this->following_prefix, $i->id);
|
|
|
|
|
|
+ Redis::zadd(self::$following_prefix, $i, $i);
|
|
});
|
|
});
|
|
- return $following;
|
|
|
|
|
|
+ return Redis::zrevrange(self::$following_prefix, $offset, $limit);
|
|
} else {
|
|
} else {
|
|
- return Redis::lrange($this->following_prefix, $offset, $limit);
|
|
|
|
|
|
+ return Redis::zrevrange(self::$following_prefix, $offset, $limit);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static function follows(string $actor, string $target)
|
|
|
|
+ {
|
|
|
|
+ $key = self::$follower_prefix . $target;
|
|
|
|
+ if(Redis::zcard($key) == 0) {
|
|
|
|
+ $p = Profile::findOrFail($target);
|
|
|
|
+ self::build()->profile($p)->followers(1);
|
|
|
|
+ self::build()->profile($p)->following(1);
|
|
|
|
+ return (bool) Redis::zrank($key, $actor);
|
|
|
|
+ } else {
|
|
|
|
+ return (bool) Redis::zrank($key, $actor);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|