class Friend{
public $flag = ""; #存储当前对象的标志信息
public $redis = ""; #redis对象成员
function __construct($name){
$this->flag = $name;
}
function getRd(){ #实例化redis对象
//实例化redis对象并连接redis服务
$this->redis = new Redis();
$this->redis -> connect('192.168.9.38',6379);
$this->redis -> select(9);
return $this->redis;
}
function follow($user){ #实现关注效果
//redis对象->sadd(本人,$user);
//$this->getRd()->sadd(本人key,$user);
$this->getRd()->sadd("user:{$this->flag}:follow", $user);
}
function following(){ #获得关注全部人员的信息
//获得当前作者本人关注的全部对象信息
//redis对象->smember(本人)
return $this->getRd()->smembers("user:{$this->flag}:follow");
}
function isfollow($user){ #判断是否关注该用户
return $this->getRd()->sismember("user:{$this->flag}:follow",$user);
}
function follow_common($user){ #获得共同关注好友信息
//当前用户与指定用户共同关注好友信息
return $this->getRd()->sinter("user:{$this->flag}:follow","user:{$user}:follow");
}
}
$user1 = new Friend(1);
$user1 -> follow(3);
$user1 -> follow(4);
$user1 -> follow(5);
$user1 -> follow(7);
$user1 -> follow(9);
$user2 = new Friend(2);
$user2 -> follow(3);
$user2 -> follow(4);
$user2 -> follow(5);
$user2 -> follow(11);
$user2 -> follow(100);
$user2 -> follow(130);
echo "user1:following:";
print_r($user1->following());
print_r($user1->isfollow(3));
var_dump($user1->isfollow(13));
print_r($user1->follow_common(2));
Redis----计算好友关注关系
最新推荐文章于 2024-01-19 15:35:07 发布