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); }
# #
/** * @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('time:',$article,$key) < $cutoff){ var_dump("该文章已过投票时间!");exit; } $article_id = explode(':',$article)['1']; if($redis->sadd('voted:'.$article_id,$user,$key)){ $redis -> zincrby('score:',$article,1,$key); $redis -> hincrby($article,'votes',1,$key); }else{ var_dump("您已经投过票了!");exit; } }
/** * @data 文章列表分页 * 对文章评分有序集合或者时间发布有序集合做分页处理,获取文章ID后,去文章详情散列表中查询该文章详情 * @author Lorne * @date 2017-03-03 */ public function get_articles($page =1,$orders =''){ $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['id'] = $val; $articles[] = $data; } return $articles; }
/** * @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('group:'.$val,$article,$db); } foreach($to_remove as $key=>$val){ $redis -> srem('grouo:'.$val,$article,$db); } }
/** * @data 组集合中的文章根据评分或者时间分页排序 * * @author Lorne * @date 2017-03-03 */ public function get_grouop_articles($orders = "time:"){ $redis = $this -> redis; $db = "queue"; $group = '开发'; $key = $orders.$group; if($redis -> exists($key,$db)){ $argument = 2; $data = $redis -> zinterstore($key,$argument,['group:'.$group,$orders],$db); //$this -> expire($key,60,$db); } return $this->get_articles(2,$key); }