thinkphp5?操作redis?實(shí)現(xiàn)文章的熱度排行和點(diǎn)贊排行的示例
使用redis hash散列 和zset有序集合實(shí)現(xiàn)文章的熱度排行和點(diǎn)贊排行 1.首先為文章建立散列,存入基本信息。 mysql簡單設(shè)計

獲取所有文章,并存入redis
//所有新聞頁
public function news()
{
$redis = new Redis();
$list = News::select();
foreach ($list as &$row){
//將所有數(shù)據(jù)存到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);//返回給前端所有數(shù)據(jù);
}2.初始化所有文章的瀏覽數(shù)和點(diǎn)贊數(shù)
//初始化文章熱度和點(diǎn)贊數(shù)
public function startNews()
{
$redis= new Redis();
$list = News::select();
foreach ($list as &$row){
//為每個文章添加熱度
$redis->zAdd('hot','0','newsId-'.$row->id);
//為每個文章添加點(diǎn)贊數(shù)
$redis->zAdd('good','0','newsId-'.$row->id);
}
dump($redis->zRange('hot','0','-1',true));
dump($redis->zRange('good','0','-1',true));
}結(jié)果
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正常讀取信息返回給前端,(此處不做代碼實(shí)現(xiàn))。然后熱度排行自動增長1.
$redis = new Redis();
$param = $this->request->param();
$news_id = $param['news_id'];
$list = News::where('id',$news_id)->find();//數(shù)據(jù)庫查到的信息,返回給前端
$redis->zIncRby('hot','1','newsId-'.$news_id);//redis數(shù)據(jù)增長1
return json($list);4.點(diǎn)贊新聞
public function newsGoods()
{
$redis = new Redis();
$param = $this->request->param();
$news_id = $param['news_id'];
$redis->zIncRby('good','1','newsId-'.$news_id);//redis數(shù)據(jù)增長1
}這時候基本業(yè)務(wù)代碼已經(jīng)完事。然后開始查看排行。首先測試查看一下熱度排行和點(diǎn)贊排行的文章。
dump($redis->zRange('hot','0','-1',true));//查看熱度排行
dump($redis->zRange('good','0','-1',true));//查看點(diǎn)贊排行結(jié)果
熱度
array(5) {
["newsId-3"] => float(2)
["newsId-4"] => float(4)
["newsId-1"] => float(6)
["newsId-2"] => float(9)
["newsId-5"] => float(16)
}
點(diǎn)贊
array(5) {
["newsId-1"] => float(3)
["newsId-2"] => float(8)
["newsId-3"] => float(10)
["newsId-4"] => float(14)
["newsId-5"] => float(48)
}
5.查看熱度排行榜(包括新聞簡介)注:可能有一種情況發(fā)生,文章id存在于排行榜中,但是對應(yīng)文章的簡介不在內(nèi)存中,那就需要去數(shù)據(jù)庫重新查此id文章的內(nèi)容(我已經(jīng)刪除了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){
//緩存里沒有該信息,去數(shù)據(jù)庫查找
$id = trim($id,'newsId-');//切割字符串。獲得文章id
$row = Db::name('news')->where('id',$id)->find();
}
}
dump($rank);
}結(jié)果
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內(nèi)存中不存在,重新再數(shù)據(jù)庫中查到的數(shù)據(jù)
6.查看點(diǎn)贊排行榜(包括新聞簡介)
//點(diǎn)贊熱度排行
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){
//緩存里沒有該信息,去數(shù)據(jù)庫查找
$id = trim($id,'newsId-');//切割字符串。獲得文章id
$row = Db::name('news')->where('id',$id)->find();
}
}
dump($rank);
}結(jié)果:
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"
}}
到此這篇關(guān)于thinkphp5 操作redis 實(shí)現(xiàn)文章的熱度排行和點(diǎn)贊排行的示例的文章就介紹到這了,更多相關(guān)thinkphp5 熱度排行和點(diǎn)贊排行內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
php中Swoole的熱更新實(shí)現(xiàn)代碼實(shí)例
這篇文章主要介紹了php中SWOOLE的熱更新實(shí)現(xiàn)代碼實(shí)例,熱更新是比較常用的技術(shù),有需要同學(xué)可以研究下2021-03-03
php+js實(shí)現(xiàn)異步圖片上傳實(shí)例分享
本來想用插件上傳圖片的,后來自己寫了一個簡單的js實(shí)現(xiàn)異步的圖片上傳,不多說上代碼非常簡單2014-06-06
php_screw安裝使用教程(另一個PHP代碼加密實(shí)現(xiàn))
這篇文章主要介紹了php_screw安裝使用教程,php_screw是另一個PHP代碼加密實(shí)現(xiàn),和Zend的encoder類似,需要的朋友可以參考下2014-05-05
PHP迭代器實(shí)現(xiàn)斐波納契數(shù)列的函數(shù)
斐波納契數(shù)列通常做法是用遞歸實(shí)現(xiàn),當(dāng)然還有其它的方法。這里現(xiàn)學(xué)現(xiàn)賣,用PHP的迭代器來實(shí)現(xiàn)一個斐波納契數(shù)列,幾乎沒有什么難度,只是把類里的next()方法重寫了一次。注釋已經(jīng)寫到代碼中,也是相當(dāng)好理解的2013-11-11

