本文实例为大家分享了PHP实现文章发布系统、用户投票系统的具体代码,供大家参考,具体内容如下
//<a href="https://www.jb51.cc/tag/wenzhang/" target="_blank" class="keywords">文章</a>已投票<a href="https://www.jb51.cc/tag/yonghuming/" target="_blank" class="keywords">用户名</a>单
$voted = "voted:".$article_id;
$this->redis->sadd($voted,$user,$key);
//设置过期时间(为1周)
$this->redis->expipre($voted,$ONE_WEEK_IN_SECONDS,$key);
//<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>现在的时间
$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);
//将<a href="https://www.jb51.cc/tag/wenzhang/" target="_blank" class="keywords">文章</a><a href="https://www.jb51.cc/tag/tianjia/" target="_blank" class="keywords">添加</a>到根据时间排序有序集合和根据评分排序有序结合中
$this -> redis -> zadd("<a href="https://www.jb51.cc/tag/score/" target="_blank" class="keywords">score</a>:",1,$article,$key);
$this -> redis -> zadd("time:",$now,$key);
$redis -> exec($key);
}
<div class="jb51code">
<pre class="brush:php;">
/**
-
@data 用户投票
-
获取文章的ID,用户ID,判断该篇文章是否已经过了投票时间,再判断用户是否已经投过票
-
写入文章对应投票用户表中(voted:文章ID),对应的文章评分加,文章详情内容中的votes统计加1
-
@author Lorne
-
@date 2017-03-03
/
public function article_vote(){
$ONE_WEEK_IN_SECONDS= 786400;
$article = "article:3";
$user = "user:7777";$redis = $this -> redis;
$key= "queue";
$cutoff = time() - $ONE_WEEK_IN_SECONDS;
//文章发布时间和投票截止日期对比
if($redis->zscore('time:',$key) < $cutoff){
var_dump("该文章已过投票时间!");exit;
}
$article_id = explode(':',$article)['1'];
if($redis->sadd('voted:'.$article_id,$key)){
$redis -> zincrby('score:',$key);
$redis -> hincrby($article,'votes',$key);
}else{
var_dump("您已经投过票了!");exit;
}
}