redis应用之文章集赞

页面展示:

上代码:

<?php

class Good
{
    public $redis = null;

    //60*60*24/20=4320,每个点赞得到的分数。
    public $score = 4320;

    //点赞增加数,或者点hate增加数
    public $num = 1;

    //init redis
    public $redis_host = "127.0.0.1";
    public $redis_port = "6379";
    public $redis_pass = "";
    const COMMENT_RECORD = 'comment:record';
    const COMMENT_SCORE = 'comment:score';

    public function __construct()
    {
        $this->redis = new Redis();
        $this->redis->connect($this->redis_host,$this->redis_port);
        $this->redis->auth($this->redis_pass);
    }

    /**
     * @param int $user_id 用户id
     * @param int $type 点击的类型 1.点like,2.点hate
     * @param int $comment_id 文章id
     * @return string json;
     */
    public function click($user_id,$type,$comment_id)
    {
        //判断是否需要更新数据
        if (!$this->ifUploadList($comment_id)) {
            return ['code'=>400,'msg'=>'文章不存在'];
        }
        $type == 1 ? $key = "like" : $key = "hate";
        $type == 1 ? $key2 = "hate" : $key2 = "like";

        //判断redis是否已经缓存了该文章数据
        //使用:分隔符对redis管理是友好的
        //这里使用redis zset-> zscore()方法
        $data = [];
        if ($this->redis->zscore("comment:like", $comment_id) || $this->redis->zscore("comment:hate", $comment_id)) {
            //已经存在
            //判断点的是什么
            $rel = $this->redis->hget(self::COMMENT_RECORD, $user_id . ":" . $comment_id);//redis hash-> hget()
            //判断以前是否点过,点的是什么?
            switch ($rel) {
                case '':
                    //什么都没点过
                    //点赞加1
                    $this->redis->zincrby("comment:".$key, $this->num, $comment_id);
                    //增加分数
                    $this->redis->zincrby(self::COMMENT_SCORE, $this->score, $comment_id);
                    //记录上次操作
                    $this->redis->hset(self::COMMENT_RECORD, $user_id . ":" . $comment_id, $type);

                    $data = array(
                        "state" => 1,
                        "msg" => $key."+1",
                    );
                    break;
                case $type:
                    //点过赞了
                    //点赞减1
                    $this->redis->zincrby("comment:".$key, -($this->num), $comment_id);
                    //增加分数
                    $this->redis->zincrby(self::COMMENT_SCORE, -($this->score), $comment_id);
                    //删除记录
                    $this->redis->hDel(self::COMMENT_RECORD, $user_id . ":" . $comment_id, $type);
                    $data = array(
                        "state" => 2,
                        "msg" => $key."-1",
                    );
                    break;
                case $type == 1 ? 2 : 1:
                    //点过hate
                    //hate减1
                    $this->redis->zincrby("comment:".$key2, -($this->num), $comment_id);
                    //增加分数
                    $this->redis->zincrby(self::COMMENT_SCORE, $this->score + $this->score, $comment_id);
                    //点赞加1
                    $this->redis->zincrby("comment:".$key, $this->num, $comment_id);
                    //记录上次操作
                    $this->redis->hset(self::COMMENT_RECORD, $user_id . ":" . $comment_id, $type);

                    $data = array(
                        "state" => 3,
                        "msg" => $key."+1",
                    );
                    break;

            }
        } else {
            //未存在
            //点赞加一
            $this->redis->zincrby("comment:".$key, $this->num, $comment_id);
            //分数增加
            $this->redis->zincrby(self::COMMENT_SCORE, $this->score, $comment_id);
            $data = array(
                "state" => 7,
                "msg" => $key."+1",
            );

            //记录
            $this->redis->hset(self::COMMENT_RECORD, $user_id . ":" . $comment_id, $type);
        }
        $list = $this->redis->zRange("comment:like",0,-1,1);
        $list1 = $this->redis->zRange("comment:hate",0,-1,1);
        foreach ($list as $k =>$v){if ($k==$comment_id){$data['like']=$v;}}
        foreach ($list1 as $k =>$v){if ($k==$comment_id){$data['hate']=$v;}}
        return $data;
    }

    /**
     * 判断文章是否存在
     * @param $comment_id
     * @return string
     */
    public function ifUploadList($comment_id)
    {
        date_default_timezone_set("Asia/Shanghai");
        $time = strtotime(date('Y-m-d H:i:s'));

        if(!$this->redis->sismember("comment:uploadset",$comment_id))
        {
            //文章不存在集合里,需要更新
            if (in_array($comment_id,[11,22,33,44])){
                $this->redis->sadd("comment:uploadset",$comment_id);
                //更新到队列
                $data = array(
                    "id" => $comment_id,
                    "time" => $time,
                );
                $json = json_encode($data);
                $this->redis->lpush("comment:uploadlist",$json);
                return true;
            }
            return false;
        }
        return true;
    }

    public function dell(){
        $this->redis->flushAll();
    }
}

//调用
$data = $_POST;
$user_id = isset($data['user_id']) ? $data['user_id'] : '';
$comment_id= isset($data['comment_id']) ? $data['comment_id'] : '请选择';
if (isset($data['like'])){
    $type = 1;
}else if (isset($data['hate'])){
    $type = 2;
}
if ($user_id && $comment_id){
    $good = new Good();
    $rel = $good->click($user_id,$type,$comment_id);
}else{
    echo '用户ID和文章id不能为空';
}

?>

<form action="a.php" method="post" style="margin-left: 700px;">
    用户id:
    <input type="text" name="user_id" placeholder="用户id" value="<?=$user_id?>"><br>
    文章标题:
    <select name="comment_id">
        <option value="<?=$comment_id?>"><?=$comment_id?></option>
        <option value="11">11</option>
        <option value="22">22</option>
        <option value="33">33</option>
        <option value="44">44</option>
    </select><br><br>
    <input type="submit" value="like" name="like" style="margin-left: 150px">
    <input type="submit" value="hate" name="hate">
    <div style="margin-top: 20px;background: #1acbfc;width: 250px">
        点赞人数:<?=isset($rel['like']) ?$rel['like'] : 0 ?><br>
        吐槽人数:<?=isset($rel['hate']) ?$rel['hate'] : 0 ?>
    </div>
</form>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值