thinkphp5 热度排行和点赞排行
seaiio 人气:0使用redis hash散列 和zset有序集合实现文章的热度排行和点赞排行 1.首先为文章建立散列,存入基本信息。 mysql简单设计
获取所有文章,并存入redis
//所有新闻页 public function news() { $redis = new Redis(); $list = News::select(); foreach ($list as &$row){ //将所有数据存到hash散列里,用于显示简介信息 $redis->handler()->hmset('newsId-'.$row->id,array('id'=>$row->id,'title'=>$row->title,'name'=>$row->name,'create_time'=>$row->create_time)); } return json($list);//返回给前端所有数据; }
2.初始化所有文章的浏览数和点赞数
//初始化文章热度和点赞数 public function startNews() { $redis= new Redis(); $list = News::select(); foreach ($list as &$row){ //为每个文章添加热度 $redis->zAdd('hot','0','newsId-'.$row->id); //为每个文章添加点赞数 $redis->zAdd('good','0','newsId-'.$row->id); } dump($redis->zRange('hot','0','-1',true)); dump($redis->zRange('good','0','-1',true)); }
结果
array(5) {
["newsId1"] => float(0)
["newsId2"] => float(0)
["newsId3"] => float(0)
["newsId4"] => float(0)
["newsId5"] => float(0)
}
array(5) {
["newsId1"] => float(0)
["newsId2"] => float(0)
["newsId3"] => float(0)
["newsId4"] => float(0)
["newsId5"] => float(0)
}
3.访问新闻 访问新闻时,mysql正常读取信息返回给前端,(此处不做代码实现)。然后热度排行自动增长1.
$redis = new Redis(); $param = $this->request->param(); $news_id = $param['news_id']; $list = News::where('id',$news_id)->find();//数据库查到的信息,返回给前端 $redis->zIncRby('hot','1','newsId-'.$news_id);//redis数据增长1 return json($list);
4.点赞新闻
public function newsGoods() { $redis = new Redis(); $param = $this->request->param(); $news_id = $param['news_id']; $redis->zIncRby('good','1','newsId-'.$news_id);//redis数据增长1 }
这时候基本业务代码已经完事。然后开始查看排行。首先测试查看一下热度排行和点赞排行的文章。
dump($redis->zRange('hot','0','-1',true));//查看热度排行 dump($redis->zRange('good','0','-1',true));//查看点赞排行
结果
热度
array(5) {
["newsId-3"] => float(2)
["newsId-4"] => float(4)
["newsId-1"] => float(6)
["newsId-2"] => float(9)
["newsId-5"] => float(16)
}
点赞
array(5) {
["newsId-1"] => float(3)
["newsId-2"] => float(8)
["newsId-3"] => float(10)
["newsId-4"] => float(14)
["newsId-5"] => float(48)
}
5.查看热度排行榜(包括新闻简介)注:可能有一种情况发生,文章id存在于排行榜中,但是对应文章的简介不在内存中,那就需要去数据库重新查此id文章的内容(我已经删除了newsId-2的简介)
//访问热度排行 public function newsHot() { $redis = new Redis(); $rank = $redis->handler()->zrevrange('hot','0','2'); foreach ( $rank as &$row ){ $id = $row; $row = $redis->hGetAll($row);//去hash散列里取得之前存好的文章简介 if(!$row){ //缓存里没有该信息,去数据库查找 $id = trim($id,'newsId-');//切割字符串。获得文章id $row = Db::name('news')->where('id',$id)->find(); } } dump($rank); }
结果
array(3) {
[0] => array(4) {
["title"] => string(15) "第五个文章"
["name"] => string(7) "作者5"
["create_time"] => string(19) "2019-11-28 14:33:43"
["id"] => string(1) "5"
}
[1] => array(7) {
["id"] => int(2)
["title"] => string(15) "第二个文章"
["name"] => string(7) "作者2"
["detail"] => string(8) "详情22"
["create_time"] => string(19) "2019-11-28 14:33:43"
["hot"] => int(0)
["good"] => int(0)
}
[2] => &array(4) {
["title"] => string(7) "文章1"
["name"] => string(6) "作者"
["create_time"] => string(19) "2019-11-28 14:33:43"
["id"] => string(1) "1"
}
}
其中第二个文章在redis内存中不存在,重新再数据库中查到的数据
6.查看点赞排行榜(包括新闻简介)
//点赞热度排行 public function newsGood() { $redis = new Redis(); $rank = $redis->handler()->zrevrange('good','0','2'); foreach ($rank as &$row){ $id = $row; $row = $redis->hGetAll($row);//去hash散列里取得之前存好的文章简介 if(!$row){ //缓存里没有该信息,去数据库查找 $id = trim($id,'newsId-');//切割字符串。获得文章id $row = Db::name('news')->where('id',$id)->find(); } } dump($rank); }
结果:
array(3) {
[0] => array(4) {
["title"] => string(15) "第五个文章"
["name"] => string(7) "作者5"
["create_time"] => string(19) "2019-11-28 14:33:43"
["id"] => string(1) "5"
}
[1] => array(4) {
["title"] => string(15) "第四个文章"
["name"] => string(7) "作者4"
["create_time"] => string(19) "2019-11-28 14:33:43"
["id"] => string(1) "4"
}
[2] => &array(4) {
["title"] => string(15) "第三个文章"
["name"] => string(7) "作者3"
["create_time"] => string(19) "2019-11-28 14:33:43"
["id"] => string(1) "3"
}}
加载全部内容