Redis in Action 文章投票
原书用 Python 与 Redis 进行交互,我用 PHP 来实现。
环境:LNMP(CentOS 6.6 + Nginx 1.8.0 + MySQL 5.6.23 + PHP 5.6.9)+ Redis 3.0.7 + phpredis 2.2.4
首先在 Linux 开启 Redis 服务:
[root@localhost ~]# cd /usr/local/redis/
[root@localhost redis]# ./bin/redis-server ./etc/redis.conf
[root@localhost redis]# ps aux|grep redis
如果显示:
root 2239 0.2 0.1 35556 1744 ? Ssl 12:08 0:00 ./bin/redis-server *:6379
root 2243 0.0 0.0 5976 724 pts/0 S+ 12:08 0:00 grep redis
说明 Redis 服务已经开启,端口号 6379
redis.php
<?php // 一周的秒数
$seconds = 7 * 86400;
define(ONE_WEEK_IN_SECONDS, $seconds);
// 每投一票文章加的分值
define(VOTE_SCORE, 432); // 实例化Redis对象
$redis = new Redis();
// 连接到Redis服务器
$conn = $redis->connect('localhost', 6379);
init_data.php 用于添加案例的数据
<?php /* 为投票网站准备数据 */ require 'redis.php'; //1.根据发布时间排序文章的有序集合 zset
$article_info = [
'100408'=>[
'time'=>strtotime('2016-4-10'),
'score'=>1332164063.49
],
'100635'=>[
'time'=>strtotime('2016-4-28'),
'score'=>1332174713.47
],
'100716'=>[
'time'=>strtotime('2016-4-29'),
'score'=>1332225027.26
]
]; foreach($article_info as $key => $val) {
$redis->zadd('time:', $val['time'], 'article:'.$key);
} //2.根据评分排序文章的有序集合 zset
foreach($article_info as $key => $val) {
$redis->zadd('score:', $val['score'], 'article:'.$key);
} //3.为某篇文章(id:100408)投过票的用户集合 set
$users = [234487, 253378, 364680, 132097, 350917];
foreach($users as $key => $val) {
$redis->sadd('voted:100408', $val);
}
vote.php 用于给文章投票,其中文章 id(article_id)和投票用户 id(user_id)通过 get 方式传递(代码清单 1-6 article_vote() 函数)
<?php header('Content-type:text/html;charset=utf-8'); require 'redis.php'; $user_id = empty($_GET['user_id']) ? 0 : (int)$_GET['user_id'];
$article_id = empty($_GET['article_id']) ? 0 : (int)$_GET['article_id']; function article_vote($redis, $user_id, $article_id) {
$cutoff = time() - ONE_WEEK_IN_SECONDS;
// 127.0.0.1:6379> zscore time article:100408
// "1460217600"
if(intval($redis->zscore('time:', 'article:'.$article_id)) < $cutoff) {
return false;
} if($redis->sadd('voted:'.$article_id, $user_id)) {
// ZINCRBY key increment member
// 为有序集 score 的成员 article:100408 的 score 值加上增量 increment
$score_new = $redis->zincrby('score:', VOTE_SCORE, 'article:'.$article_id);
echo $score_new;
// HINCRBY key field increment
// 为哈希表key中的域field的值加上增量increment。
// 如果用户是第一次为这篇文章投票,那么增加这篇文章的投票数量和评分 } else {
return false;
}
return true;
} if(! article_vote($redis, $user_id, $article_id)) {
echo '投票失败';
} else {
echo '投票成功';
}
执行 http://yourdomain/init_data.php,完成 Redis 的连接和数据的初始化,可以进入 Redis 的客户端查询文章投票积分的有序集合(zset)和文章 100408 的投票用户的集合(set):
[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379> zrange score: 0 -1 withscores
1) "article:100408"
2) "1332164063.49"
3) "article:100635"
4) "1332174713.47"
5) "article:100716"
6) "1332225027.26"
127.0.0.1:6379> zrange time: 0 -1 withscores
1) "article:100408"
2) "1460217600"
3) "article:100635"
4) "1461772800"
5) "article:100716"
6) "1461859200"
然后访问 http://yourdomain/vote.php?user_id=100&article_id=100408 让 user_id 为 100 的用户给编号100408 的文章投票。
再次进入 Redis 的客户端查询文章投票积分的有序集合(zset)和文章 100408 的投票用户的集合(set):
127.0.0.1:6379> zrange score: 0 -1 withscores
1) "article:100408"
2) "1332164495.49"
3) "article:100635"
4) "1332174713.47"
5) "article:100716"
6) "1332225027.26"
127.0.0.1:6379> smembers voted:100408
1) "100"
2) "132097"
3) "234487"
4) "253378"
5) "350917"
6) "364680"
127.0.0.1:6379>
发布文章 post_article.php(代码清单 1-7 post_article() 函数)
<?php require 'redis.php'; // @param object $redis redis对象
// @param int $user_id 用户编号
// @param string $title 文章标题
// @param string $link 文章链接
function post_article($redis, $user_id, $title, $link) {
$article_id = $redis->incr('article:'); // 生成新的文章id $voted = 'voted:'.$article_id;
$redis->sadd($voted, $user_id); // 将发布文章的用户添加到文章已投票的用户名单中
$redis->expire($voted, ONE_WEEK_IN_SECONDS); // 将投票名单的过期时间设置为一周 $now = time();
$article = 'article:'.$article_id;
$redis->hmset($article, [
'title' => $title,
'link' => $link,
'poster' => $user_id,
'time' => $now,
'votes'=> 1
]); // 将文章的信息存储到一个散列里 $redis->zadd('score:', $now + VOTE_SCORE, $article); // 把文章添加到根据评分排序的有序集合中
$redis->zadd('time:', $now, $article); // 把文章添加到根据发布时间排序的有序集合中 return $article_id;
} $user_id = isset($_GET['user_id']) ? $_GET['user_id'] : 0;
$mtid = mt_rand(0,999);
$title = '文章标题'.$mtid;
$link = 'http://www.youdomain.com/article/'.$mtid;
if(post_article($redis, $user_id, $title, $link)) {
echo 'success';
} else {
echo 'error';
}
访问:http://yourdomain/post_article.php
由于 url 不带参数并且 Redis 中不存在 article: ,因此会有一个 user_id 为 0 的用户发布 article:1
此时查询 Redis 中时间和分数的有序集合、article:1 的投票用户集合以及 article:1 的散列内容:
127.0.0.1:6379> zrange time: 0 -1 withscores
1) "article:100408"
2) "1460217600"
3) "article:100635"
4) "1461772800"
5) "article:100716"
6) "1461859200"
7) "article:1"
8) "1465105632"
127.0.0.1:6379> zrange score: 0 -1 withscores
1) "article:100408"
2) "1332164495.49"
3) "article:100635"
4) "1332174713.47"
5) "article:100716"
6) "1332225027.26"
7) "article:1"
8) "1465106064"
127.0.0.1:6379> smembers voted:1
1) "0"
127.0.0.1:6379> hgetall article:1
1) "title"
2) "\xe6\x96\x87\xe7\xab\xa0\xe6\xa0\x87\xe9\xa2\x9868"
3) "link"
4) "http://www.youdomain.com/article/68"
5) "poster"
6) "0"
7) "time"
8) "1465105632"
9) "votes"
10) "1"
可以发布多篇文章用于测试。
get_articles.php 获取文章列表(代码清单1-8 get_article() 函数)
<?php require 'redis.php'; define(ARTICLES_PER_PAGE, 5); // 每页5条数据 // @param object $redis redis对象
// @param int $page 当前页
// @param string $order 根据$order来排序
// @return array $articles 文章信息
function get_articles($redis, $page, $order = 'score:') {
$start = ($page - 1) * ARTICLES_PER_PAGE;
$end = $start + ARTICLES_PER_PAGE - 1; $ids = $redis->zrevrange($order, $start, $end); // 获取多个文件的序号 按 score 值递减(从大到小)来排列。
$articles = [];
foreach($ids as $id) {
$article_data = $redis->hgetall($id);
$article_data['id'] = $id;
$articles[] = $article_data;
} return $articles;
} $page = isset($_GET['page']) ? $_GET['page'] : 1;
$articles = get_articles($redis, $page); echo '<pre>';
print_r($articles);
可以通过 http://yourdomain/get_articles.php 或 http://yourdomain/get_articles.php?page = 2 来获取文章列表,可以得到:
Array
(
[0] => Array
(
[title] => 文章标题23
[link] => http://www.youdomain.com/article/23
[poster] => 106
[time] => 1465180517
[votes] => 1
[id] => article:8
) [1] => Array
(
[title] => 文章标题719
[link] => http://www.youdomain.com/article/719
[poster] => 105
[time] => 1465180514
[votes] => 1
[id] => article:7
) [2] => Array
(
[title] => 文章标题811
[link] => http://www.youdomain.com/article/811
[poster] => 104
[time] => 1465180511
[votes] => 1
[id] => article:6
) [3] => Array
(
[title] => 文章标题22
[link] => http://www.youdomain.com/article/22
[poster] => 103
[time] => 1465180508
[votes] => 1
[id] => article:5
) [4] => Array
(
[title] => 文章标题350
[link] => http://www.youdomain.com/article/350
[poster] => 102
[time] => 1465180506
[votes] => 1
[id] => article:4
) )
添加文章至分组以及从分组中移除文章(代码清单:1-9 add_remove_groups() 函数)
<?php require 'redis.php'; // @param object $redis
// @param int $artile_id
// @param array $to_add
// @param array $to_remove
function add_remove_groups($redis, $article_id, $to_add = [], $to_remove = []) {
$article = 'article:'.$article_id; // 将文章添加到所属的群组
if(! empty($to_add)) {
foreach($to_add as $group) {
$redis->sadd('group:'.$group, $article);
}
} // 将文章从群组里面移除
if(! empty($to_remove)) {
foreach($to_remove as $group) {
$redis->srem('group'.$group, $article);
}
}
}
初始化群组数据 init_data_group.php
<?php /* 准备群组数据 */ require 'redis.php'; $article_info = ['article:100408', 'article:100635', 'article:5']; foreach($article_info as $key => $val) {
$redis->sadd('group:programming', $val);
}
代码清单 1-10 get_group_articles 函数
function get_group_article($redis, $group, $page, $order = 'score:') {
$key = $order.$group; // 为每个群组的每种排列顺序都创建一个键
if(! $redis->exists($key)) { // 检查是否已有缓存的排序结果,如果没有的话则进行排序
$redis->zInter($key, array('group:'.$group, $order), array(1, 1), $aggregate = 'max');
$redis->expire($key, 60); // 60s之后redis删除有序集合
}
return get_articles($redis, $page, $key);
} $group = 'programming';
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$articles = get_group_article($redis, $group, $page); echo '<pre>';
print_r($articles);
附:
文章案例来自《Redis 实战》
phpredis 文档:https://github.com/phpredis/phpredis
Redis 命令参考:http://doc.redisfans.com/sorted_set/zrange.html
php-redis 中文文档:http://www.cnblogs.com/weafer/archive/2011/09/21/2184059.html
redis在PHP中的基本使用案例:http://www.t086.com/article/4901
Redis in Action 文章投票的更多相关文章
- Redis 实战 —— 02. Redis 简单实践 - 文章投票
需求 功能: P15 发布文章 获取文章 文章分组 投支持票 数值及限制条件 P15 如果一篇文章获得了至少 200 张支持票,那么这篇文章就是一篇有趣的文章 如果这个网站每天有 50 篇有趣的文章, ...
- .Net Redis实战——实现文章投票并排序
本系列文章为学习Redis实战一书记录的随笔. 软件和环境版本:Redis:5.0.7 .Net 5.0 文中不会对Redis基础概念做过多介绍. Redis数据类型和命令可在菜鸟教程学习:http ...
- 使用redis构建文章投票系统
首先,我得说明这篇博客基本上就是<<redis in action>>第一章内容的读书笔记. 需求 首先,说明一下,我们的需求 用户可以发表文章,发表时,自己就默认的给自己的文 ...
- redis 实例2 构建文章投票网站后端
redis 实例2 构建文章投票网站后端 1.限制条件 一.如果网站获得200张支持票,那么这篇文章被设置成有趣的文章 二.如果网站发布的文章中有一定数量被认定为有趣的文章,那么这些文章需要被设置 ...
- Redis实现文章投票功能
Redis的具体操作这里就不说了,说一下需求和设计思路. 需求:自己实现一个文章投票的功能1.能够按照时间分页倒叙查看文章信息2.能够给文章投票,一个用户给一篇文章只能投票一次3.需要记录分值.每次投 ...
- 使用Redis构建文章投票网站
涉及到的key: 1. article_time, 记录文章的发布时间,zset结构 2. article_score, 记录文章的得分, zset结构 得分 = 发布时间 + 投票用户数 X 432 ...
- Redis in Action : Redis 实战学习笔记
1 1 1 Redis in Action : Redis 实战学习笔记 1 http://redis.io/ https://github.com/antirez/redis https://ww ...
- Redis in Action
Redis in Action Redis REmote DIctionary Server(Redis) Redis 是一种开放源代码(BSD许可)的内存中数据结构存储,用作数据库,缓存和消息代理. ...
- 微博与Redis系统技术文章记录
Redis 持久化,有两种: rdb 和 aof, rdb是记录一段时间内的操作,一盘的配置是一段时间内操作超过多少次就持久化. aof可以实现每次操作都持久化. 这里我们使用aof. 配置方式,打开 ...
随机推荐
- window共享linux下的文件 samba
1.在Ubuntu上安装samba服务 sudo apt-get install samba 2.修改配置文件vim /etc/samba/smb.conf [xubu] (共享名) guest ac ...
- php之登录功能实现。
项目默认存在的东西:jquery库[jquery.min.js] 登录功能实现的基本逻辑: 1.书写前台php功能基本页面:(index.php) a.编写基本功能,比如用户名.密码.登录 b.引用j ...
- Uva 2034
求定积分 (结果当时我没看到平均值) //正常多项式求 #include<iostream> #include<cstdio> #include<cmath> us ...
- ps批量处理图片
刚刚有朋友问,ps咋做批量动作呢,其实特别简单,基本一劳永逸,用尺寸做个例子,大家看看就知道了.
- AssetBundle loading failed because.....已解决
http://blog.csdn.net/ldghd/article/details/9632455 ***************************** 一 ******* ...
- PYTHON 写函数,计算传入字符串中【数字、字母、空格、以及其他的个数】
def func1(s): al_num = 0 spance_num = 0 digit_num = 0 others_num = 0 for i in s: if i.isdigit(): # i ...
- CLR VIA C# 学习笔记
第19章 可空类型 1)使用Nullable<T>可将int32的值类型设置为Null,CLR会在Null时默认赋值为0; 如:Nullable<T> x=null; //使用 ...
- TreeView checkbox 全选
在使用TreeView 控件 ,进行权限管理的时候,需要使用 checkbox全选. 勾选父节点,子节点全部选中.取消父节点,子节点不选中. 勾选子节点,父节点也选中. 以下是在使用的例子: < ...
- 提交form表单
方法一: $.ajax({ }) $.ajax({ cache: true, type: "POST", url:ajaxCallUrl, data:$('#yourformid' ...
- 《DSP using MATLAB》示例Example5.13