【redis进阶】五、简单实例

namespace app\redis\controller;


use Predis\Client;
use think\Controller;

class Tools extends Controller
{
    public $oneWeekInSecond = 604800;
    public $voteScore = 432;
    public $redis = '';

    public function __construct()
    {
        $redisConfig = config('redis');

        $this->redis = new Client($redisConfig);
    }

    public function index()
    {
        //发布文章
//        $this->postArticle(1, 'test1', 'test1');
//        $this->postArticle(1, 'test2', 'test2');
//        $this->postArticle(1, 'test3', 'test3');

        //对文章投票
//        $article['id'] = 1;
//        $article['name'] = 'article:1';
//        $this->articleVote(2,$article);
//        $this->articleVote(3,$article,0);

        //获取文章
//        $article = $this->getArticles(1);
//        halt($article);

    }

    //发布文章
    public function postArticle($user,$title,$link)
    {
        //生成文章id
        $articleId = $this->redis->incr('article:');

        //将发布者添加到已投票的用户组去
        $voted = 'voted:'.$articleId;
        $this->redis->sadd($voted,$user);
        $this->redis->expire($voted,$this->oneWeekInSecond);

        $article = 'article:'.$articleId;

        $this->redis->hmset($article,[
            'title' => $title,
            'link' => $link,
            'poster' => $user,
            'time' => time(),
            'votes' => 1,
            'opvotes' => 0
        ]);

        $this->redis->zadd('score:',[$article => $this->voteScore]);

        $this->redis->zadd('time:',[$article =>time()]);

        return $articleId;

    }

    //用户投票功能
    public function articleVote($user_id,$article,$type = 1)
    {
        $publishTime = $this->redis->zscore('time:',$article['name']);

        //获取文章发布时间(超过一周不再允许投票)
        if ($publishTime + $this->oneWeekInSecond < time()) {
            return;
        }

        //投赞成票
        if ($type == 1){
            $result = $this->redis->sadd('voted:'.$article['id'],$user_id);
            if ($result) {
                $this->redis->zincrby('score:',$this->voteScore,$article['name']);
                $this->redis->hincrby($article['name'],'votes',1);
            }
        }else{
            //实现反对票
            //1。检查用户是否投过赞成票
            $hasVoted = $this->redis->sismember('voted:'.$article['id'],$user_id);
            if ($hasVoted) {
                return;
            }

            $result = $this->redis->sadd('OpVoted:',$user_id);
            if ($result) {
                $this->redis->zincrby('score:',-$this->voteScore,$article['name']);
                $this->redis->hincrby($article['name'],'opvotes',1);
            }
        }
    }



    //获取文章
    public $articlePerPage = 25;
    public function getArticles($page,$order = 'score:')
    {
        $start = ($page - 1) * $this->articlePerPage;
        $end = $start + $this->articlePerPage - 1;
        $ids = $this->redis->zrevrange($order,$start,$end);

        $articles = [];

        foreach ($ids as $id){
            $articleData = $this->redis->hgetall($id);
            $articleData['id'] = $id;
            $articles[] = $articleData;
        }
        return $articles;
    }

    //添加或者删除文章分组
    public function addOrRemoveGroups($articleId,array $toAdd,array $toRemove)
    {
        $article = 'article:'.$articleId;
        foreach ($toAdd as $group){
            $this->redis->sadd('group:'.$group,$article);
        }

        foreach ($toRemove as $group){
            $this->redis->srem('group:'.$group,$article);
        }
    }

    //获取组内的文章
    public function getGroupArticles($group,$page,$order = 'score:'){
        $key = $order.$group;
        if ($this->redis->exists($key)) {
            $this->redis->zinterstore($key,['group:'.$group,$order],['aggregate' => 'max']);
            $this->redis->expire($key,60);
        }
        return $this->getArticles($page,$key);
    }


看恩吧
网站不承担任何有关评论的责任
  • 最新评论
  • 总共条评论
取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦