PHP+Redis怎么实现点赞效果

发布时间:2021-11-30 18:06:10 作者:iii
来源:亿速云 阅读:209

这篇文章主要介绍“PHP+Redis怎么实现点赞效果”,在日常操作中,相信很多人在PHP+Redis怎么实现点赞效果问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”PHP+Redis怎么实现点赞效果”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

前言

点赞其实是一个很有意思的功能。基本的设计思路有大致两种, 一种自然是用mysql(写了几百行的代码都还没写完,有毒)啦

数据库直接落地存储, 另外一种就是利用点赞的业务特征来扔到redis(或memcache)中, 然后离线刷回mysql等。

我这里所讲的功能都是基于我之前的项目去说的,所以有些地方可以不用管的,我主要是记录这个功能的实现思路,当你理解了,基本想用什么鬼语言写都一样的。

直接写入Mysql

直接写入Mysql是最简单的做法。

做三个表即可,

缺点

redis存储随后批量刷回数据库

redis主要的特点就是快, 毕竟主要数据都在内存嘛;

另外为啥我选择redis而不是memcache的主要原因在于redis支持更多的数据类型, 例如hash, set, zset等。

下面具体的会用到这几个类型。

优点

缺点

其实上面第二点缺点是可以避免的,这就涉及到redis 的一些设计模式,不懂没关系,我尽量详细的写,后面我会给出如何解决这个缺点。

设计功能前知识准备

  1.将要用到的redis数据类型(具体的类型说明,请看底部链接,有详细说明):

  2.关于热度如何去判断:

  大家都知道,文章获得点赞数越高,文章的热度就越高,那么怎么判断呢?不就直接记录点赞数就行啦,但是对于最新的文章怎么办?例如有一篇文章一年前发布的,获得50个赞,有篇最新文章获得49个赞,但是按照上面所说的一年前的文章热度还比最新的高,这就不合理了,文章都是时效性,谁都想看最新最热的。

  so!我们要换个方法去处理这个时效性,绝大部分语言都有 时间戳 生成的方法,时间戳随着时间越新,数字越大,直接将时间戳初始化赋值给文章的score,这样最新的文章相比以前的文章就会靠前了。接着是点赞对score的影响,我们假设一天得到20个赞算是一天最热,一天60*60*24=86400秒,然后得到一个赞就是得到86400 / 20 = 4320分。具体数字看自己的业务需求定,我只是举例子而已。点hate当然也会减去相应的数字。

  1. <?php


  2. class Good

  3. {

  4.     public $redis = null;


  5.     //60*60*24/20=4320,每个点赞得到的分数,反之即之。

  6.     public $score = 4320;


  7.     //点赞增加数,或者点hate增加数

  8.     public $num = 1;


  9.     //init redis

  10.     public $redis_host = "127.0.0.1";

  11.     public $redis_port = "6379";

  12.     public $redis_pass = "";


  13.     public function __construct()

  14.     {

  15.         $this->redis = new Redis();

  16.         $this->redis->connect($this->redis_host,$this->redis_port);

  17.         $this->reids->auth($this->redis_pass);

  18.     }


  19.     /**

  20.     * @param int $user_id 用户id

  21.     * @param int $type 点击的类型 1.点like,2.点hate

  22.     * @param int $comment_id 文章id

  23.     * @return string json;

  24.     */

  25.     public function click($user_id,$type,$comment_id)

  26.     {

  27.         //判断redis是否已经缓存了该文章数据

  28.         //使用:分隔符对redis管理是友好的

  29.         //这里使用redis zset-> zscore()方法

  30.         if($this->redis->zscore("comment:like",$comment_id))

  31.         {

  32.             //已经存在

  33.             //判断点的是什么

  34.             if($type==1)

  35.             {

  36.                 //判断以前是否点过,点的是什么?

  37.                 //redis hash-> hget()

  38.                 $rel = $this->redis->hget("comment:record",$user_id.":".$comment_id);

  39.                 if(!$rel)

  40.                 {

  41.                     //什么都没点过

  42.                     //点赞加1

  43.                     $this->redis->zincrby("comment:like",$this->num,$comment_id);

  44.                     //增加分数

  45.                     $this->redis->zincrby("comment:score",$this->score,$comment_id);

  46.                     //记录上次操作

  47.                     $this->redis->hset("comment:record",$user_id.":".$comment_id,$type);


  48.                     $data = array(

  49.                         "state" => 1,

  50.                         "status" => 200,

  51.                         "msg" => "like+1",

  52.                     );

  53.                 }

  54.                 else if($rel==$type)

  55.                 {

  56.                     //点过赞了

  57.                     //点赞减1

  58.                     $this->redis->zincrby("comment:like",-($this->num),$comment_id);

  59.                     //增加分数

  60.                     $this->redis->zincrby("comment:score",-($this->score),$comment_id);

  61.                     $data = array(

  62.                         "state" => 2,

  63.                         "status" => 200,

  64.                         "msg" => "like-1",

  65.                     );

  66.                 }

  67.                 else if($rel==2)

  68.                 {

  69.                     //点过hate

  70.                     //hate减1

  71.                     $this->redis->zincrby("comment:hate",-($this->num),$comment_id);

  72.                     //增加分数

  73.                     $this->redis->zincrby("comment:score",$this->score+$this->score,$comment_id);

  74.                     //点赞加1

  75.                     $this->redis->zincrby("comment:like",$this->num,$comment_id);

  76.                     //记录上次操作

  77.                     $this->redis->hset("comment:record",$user_id.":".$comment_id,$type);


  78.                     $data = array(

  79.                         "state" => 3,

  80.                         "status" => 200,

  81.                         "msg" => "like+1",

  82.                     );

  83.                 }

  84.             }

  85.             else if($type==2)

  86.             {

  87.                 //点hate和点赞的逻辑是一样的。参看上面的点赞

  88.                 $rel = $this->redis->hget("comment:record",$user_id.":".$comment_id);

  89.                 if(!$rel)

  90.                 {

  91.                     //什么都没点过

  92.                     //点hate加1

  93.                     $this->redis->zincrby("comment:hate",$this->num,$comment_id);

  94.                     //减分数

  95.                     $this->redis->zincrby("comment:score",-($this->score),$comment_id);

  96.                     //记录上次操作

  97.                     $this->redis->hset("comment:record",$user_id.":".$comment_id,$type);


  98.                     $data = array(

  99.                         "state" => 4,

  100.                         "status" => 200,

  101.                         "msg" => "hate+1",

  102.                     );

  103.                 }

  104.                 else if($rel==$type)

  105.                 {

  106.                     //点过hate了

  107.                     //点hate减1

  108.                     $this->redis->zincrby("comment:hate",-($this->num),$comment_id);

  109.                     //增加分数

  110.                     $this->redis->zincrby("comment:score",$this->score,$comment_id);


  111.                     $data = array(

  112.                         "state" => 5,

  113.                         "status" => 200,

  114.                         "msg" => "hate-1",

  115.                     );

  116.                     return $data;

  117.                 }

  118.                 else if($rel==2)

  119.                 {

  120.                     //点过like

  121.                     //like减1

  122.                     $this->redis->zincrby("comment:like",-($this->num),$comment_id);

  123.                     //增加分数

  124.                     $this->redis->zincrby("comment:score",-($this->score+$this->score),$comment_id);

  125.                     //点hate加1

  126.                     $this->redis->zincrby("comment:hate",$this->num,$comment_id);


  127.                     $data = array(

  128.                         "state" => 6,

  129.                         "status" => 200,

  130.                         "msg" => "hate+1",

  131.                     );

  132.                     return $data;

  133.                 }

  134.             }

  135.         }

  136.         else

  137.         {

  138.             //未存在

  139.             if($type==1)

  140.             {

  141.                 //点赞加一

  142.                 $this->redis->zincrby("comment:like",$this->num,$comment_id);

  143.                 //分数增加

  144.                 $this->redis->zincrby("comment:score",$this->score,$comment_id);

  145.                 $data = array(

  146.                     "state" => 7,

  147.                     "status" => 200,

  148.                     "msg" => "like+1",

  149.                 );

  150.             }

  151.             else if($type==2)

  152.             {

  153.                 //点hate加一

  154.                 $this->redis->zincrby("comment:hate",$this->num,$comment_id);

  155.                 //分数减少

  156.                 $this->redis->zincrby("comment:score",-($this->score),$comment_id);


  157.                 $data = array(

  158.                     "state" => 8,

  159.                     "status" => 200,

  160.                     "msg" => "hate+1",

  161.                 );

  162.             }

  163.             //记录

  164.             $this->redis->hset("comment:record",$user_id.":".$comment_id,$type);

  165.         }


  166.         //判断是否需要更新数据

  167.         $this->ifUploadList($comment_id);


  168.         return $data;

  169.     }


  170.     public function ifUploadList($comment_id)

  171.     {

  172.         date_default_timezone_set("Asia/Shanghai");

  173.         $time = strtotime(date('Y-m-d H:i:s'));


  174.         if(!$this->redis->sismember("comment:uploadset",$comment_id))

  175.         {

  176.             //文章不存在集合里,需要更新

  177.             $this->redis->sadd("comment:uploadset",$comment_id);

  178.             //更新到队列

  179.             $data = array(

  180.                 "id" => $comment_id,

  181.                 "time" => $time,

  182.             );

  183.             $json = json_encode($data);

  184.             $this->redis->lpush("comment:uploadlist",$json);

  185.         }

  186.     }

  187. }


  188. //调用

  189. $user_id = 100;

  190. $type = 1;

  191. $comment_id= 99;

  192. $good = new Good();

  193. $rel = $good->click($user_id,$type,$comment_id);

  194. var_dump($rel);

到此,关于“PHP+Redis怎么实现点赞效果”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. js实现点赞效果
  2. javascript实现手动点赞效果的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php redis

上一篇:关系型数据库的原理及优势是什么

下一篇:C/C++ Qt TreeWidget单层树形组件怎么应用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》