首页 > PHP教程 > php开发知识文章

php redis实现文章发布及用户投票

本文主要介绍了php实现文章发布系统、用户投票系统的具体代码,具体内容如下

采用php+redis,简易的实现文章发布系统,用户投票,文章分组,分页排序。

class Article
{
/**
* @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.yuqingqi.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);
}

}

redis是一个高性能的key-value存储系统,最为常见的5大类型:string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型),

和memcache区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。

redis集群

以上就是本文php redis实现文章发布及用户投票的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。

关闭
感谢您的支持,我会继续努力!
扫码打赏,建议金额1-10元


提醒:打赏金额将直接进入对方账号,无法退款,请您谨慎操作。