Detailed introduction to the example of php redis implementing article publishing system (user voting system)

黄舟
Release: 2023-03-06 11:48:02
Original
1749 people have browsed it

This article mainly introduces the article publishing system and user voting system implemented by php redis in detail. It has a certain reference value. Interested friends can refer to it.

The examples in this article are shared with you. The specific codes for implementing the article publishing system and user voting system in PHP are for your reference. The specific contents are as follows

/**
  * @data 文章发布
  *    文章详情散列表中递增ID,讲文章发布者ID写入投票用户集合中,设置投票时间为一周
  *    讲文章内容写入文章散列中,讲文章写入文章评分有序集合和文章发布有序集合中
  * @author Lorne
  * @date 2017-03-03
  */
  public function post_article($user){
    $VOTE_SCORE = 24;
    $redis = $this -> redis;
    $key= "queue";
    $ONE_WEEK_IN_SECONDS= 7*86400;
    $redis -> multi($key);
    //生成新的文章id
    $article_id = $redis -> incr("article:",$key);

    //文章已投票用户名单
    $voted = "voted:".$article_id;
    $this->redis->sadd($voted,$user,$key);
    //设置过期时间(为1周)
    $this->redis->expipre($voted,$ONE_WEEK_IN_SECONDS,$key);

    //获取现在的时间
    $now =time();
    $article = "article:".$article_id;
    $data = ['title'=>'测试1','link'=>'www.hahaha.com','poster'=>$user,'tine'=>$now,'votes'=>1];
    //$data = json_encode($data);
    $redis -> hmset($article,$data,$key);

    //将文章添加到根据时间排序有序集合和根据评分排序有序结合中
    $this -> redis -> zadd("score:",1,$article,$key);
    $this -> redis -> zadd("time:",$now,$article,$key);

    $redis -> exec($key);
  }
Copy after login


# #

/**
  * @data 用户投票
  *    获取文章的ID,用户ID,判断该篇文章是否已经过了投票时间,再判断用户是否已经投过票
  *    写入文章对应投票用户表中(voted:文章ID),对应的文章评分加,文章详情内容中的votes统计加1
  * @author Lorne
  * @date 2017-03-03
  */
  public function article_vote(){
    $ONE_WEEK_IN_SECONDS= 7*86400;
    $article = "article:3";
    $user = "user:7777";

    $redis = $this -> redis;
    $key= "queue";
    $cutoff = time() - $ONE_WEEK_IN_SECONDS;
    //文章发布时间和投票截止日期对比
    if($redis->zscore(&#39;time:&#39;,$article,$key) < $cutoff){
      var_dump("该文章已过投票时间!");exit;
    }
    $article_id = explode(&#39;:&#39;,$article)[&#39;1&#39;];
    if($redis->sadd(&#39;voted:&#39;.$article_id,$user,$key)){
      $redis -> zincrby(&#39;score:&#39;,$article,1,$key);
      $redis -> hincrby($article,&#39;votes&#39;,1,$key);
    }else{
      var_dump("您已经投过票了!");exit;
    }
  }
Copy after login

/**
  * @data 文章列表分页
  *    对文章评分有序集合或者时间发布有序集合做分页处理,获取文章ID后,去文章详情散列表中查询该文章详情
  * @author Lorne
  * @date 2017-03-03
  */
  public function get_articles($page =1,$orders =&#39;&#39;){
    $redis = $this->redis;
    $db = "queue";
    //$orders = "time:";
    $per_page = 3;
    $start = ($page-1)*$per_page;
    $end = $start + $per_page -1;
    $ids = $redis -> zrevrange($orders,$start,$end,$db);
    foreach($ids as $key=>$val){
      $data = $redis -> hgetall($val,$db);
      $data[&#39;id&#39;] = $val;
      $articles[] = $data;
    }
    return $articles;
  }
Copy after login

/**
  * @data 文章添加组和移除组
  *    讲该文章加入不同的分组中,或者从个分组中移除该篇文章
  * @author Lorne
  * @date 2017-03-03
  */
  public function add_remove_group($article_id,$to_add = [],$to_remove = []){
    $redis = $this->redis;
    $db = "queue";
    $article = "article:".$article_id;
    foreach($to_add as $key=>$val){
      $redis -> sadd(&#39;group:&#39;.$val,$article,$db);
    }
    foreach($to_remove as $key=>$val){
      $redis -> srem(&#39;grouo:&#39;.$val,$article,$db);
    }
  }
Copy after login

/**
  * @data 组集合中的文章根据评分或者时间分页排序
  *    
  * @author Lorne
  * @date 2017-03-03
  */
  public function get_grouop_articles($orders = "time:"){
    $redis = $this -> redis;
    $db = "queue";
    $group = &#39;开发&#39;;
    $key = $orders.$group;
    if($redis -> exists($key,$db)){
      $argument = 2;
      $data = $redis -> zinterstore($key,$argument,[&#39;group:&#39;.$group,$orders],$db);
      //$this -> expire($key,60,$db);
    }
    return $this->get_articles(2,$key);
  }
Copy after login

Using php+redis, we can easily implement article publishing system, user voting, article grouping, and paging sorting.

redis is a high-performance key-value storage system. The five most common types are: string (string), list (linked list), set (set), zset (sorted set - ordered set) ) and hash (hash type). The difference from memcache is that redis will periodically write updated data to disk or write modification operations to additional record files, and on this basis, it implements master-slave (master-slave). )Synchronize.

Recently, I like to use redis more and more. Those who are interested in it can communicate more together.

The above is php, redis, and the content of the article. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!