redis分页摘抄

Redis 笔记与总结8 PHP + Redis 信息管理系统(分页+好友关注)

分页

要对列表页进行分页,需要知道:

①用户总数 $count

② 页大小 $pageSize:用户自定义

③ 当前页:$page:GET 方式获取  

④ 总页数:$pageCount = ceil($count / $pageSize)

关键是用户总数 $count 的获取:

可以采取的方案是,在用户注册时,把 uid 存入链表,统计链表中用户 uid 的个数,即为用户总数 $count

【操作】

reg.php:(line 14)

<?php
require 'redis.php'; $username = $_POST['username'];
$password = md5($_POST['password']);
$age =$_POST['age'];
//uid 自加
//当有 userid 这个键时,执行 incr时该键的值加1;不存在该键时,创建一个 userid,值为0,执行incr,值为1
$uid = $redis->incr('userid'); //向 hash 表中批量添加数据:hMset
$redis->hMset('user:'.$uid, array('uid'=>$uid, 'name'=>$username, 'password'=>$password, 'age'=>$age));
//把用户 id 存入链表
$redis->rpush('uid', $uid); //跳转
header('location:list.php');

在 redis 客户端 flushdb 清除一下数据库,重新注册用户;

注册了 9 个用户:

127.0.0.1:6379>  keys *
1) "user:1"
2) "user:4"
3) "user:7"
4) "user:3"
5) "user:6"
6) "uid"
7) "user:8"
8) "user:5"
9) "user:9"
10) "user:2"
11) "userid"

key 为 uid 的键即是保存用户 uid 的链表:

127.0.0.1:6379> type uid
list
127.0.0.1:6379> lrange uid 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
9) "9"

  前台:redis分页摘抄

删除用户时,同时删除链表结构中的对应的用户的 uid :

del.php(line:7):

<?php
require 'redis.php'; $uid = $_GET['uid'];
$redis->del('user:'.$uid);
//删除链表中的用户 uid
$redis->lrem('uid', $uid); header('location:list.php');

  删除其中一个用户(例如秦明),在 redis 客户端获取 list uid:

127.0.0.1:6379> lrange uid 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "9"

uid 为 8 的值已经被删掉了。

在 list.php 中可以通过 $resid->lsize('uid'); 来获取 key 为 uid 的链表元素的个数(line:15),同时修改 for 循环为 foreach 循环(line:36),分页(line:13):

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户信息列表</title>
</head>
<body>
<a href="add.php">返回注册页面</a>
<a href="login.php">登录</a>
<?php
require 'redis.php'; /*分页*/
//用户总数
$count = $redis->lsize('uid');
//页大小
$pageSize = 5;
//当前页码
$page = !empty($_GET['page'])?$_GET['page']:1;
//页总数
$pageCount = ceil($count / $pageSize); /*
第 1 页:lrange 0 4
第 2 页:lrange 5 9
第 3 页:lrange 10 14
第 n 页:lrange ($page - 1) * $pageSize ($page - 1) * $pageSize + ($pageSize - 1)
*/
$ids = $redis->lrange('uid', (($page - 1) * $pageSize), (($page - 1) * $pageSize + ($pageSize - 1)));
/*
var_dump($ids);
$page = 1
array(5) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" }
*/ foreach($ids as $v){
$data[] = $redis->hgetall('user:'.$v);
}
?>
<table border="1" cellspacing="0" cellpadding="5">
<tr>
<th>uid</th>
<th>name</th>
<th>age</th>
<th>操作</th>
</tr>
<?php foreach($data as $v){ ?>
<tr>
<td><?php echo $v['uid']?></td>
<td><?php echo $v['name']?></td>
<td><?php echo $v['age']?></td>
<td><a href="del.php?uid=<?php echo $v['uid'];?>">删除</a> | <a href="mod.php?uid=<?php echo $v['uid'];?>">编辑</a></td>
</tr>
<?php } ?>
<tr><td colspan="4">
<a href="?page=<?php echo ($page-1)<=1?1:($page-1);?>">上一页</a>
<a href="?page=<?php echo ($page+1)>$pageCount?$pageCount:($page+1);?>">下一页 </a>
<a href="?page=1">首页</a>
<a href="?page=<?php echo $pageCount;?>">尾页</a>
当前<?php echo $page;?>页
总共<?php echo $pageCount;?>页
共<?php echo $count;?>用户
</td>
</tr>
</table>
</body>
</html>

redis分页摘抄

完成

登录

需要判断用户输入的用户名是否已经注册,实现的方法是:在用户注册的时候把用户名存入字符串,修改 reg.php(注册):

reg.php(line:15):

<?php
require 'redis.php'; $username = $_POST['username'];
$password = md5($_POST['password']);
$age =$_POST['age'];
//uid 自加
//当有 userid 这个键时,执行 incr时该键的值加1;不存在该键时,创建一个 userid,值为0,执行incr,值为1
$uid = $redis->incr('userid'); //向 hash 表中批量添加数据:hMset
$redis->hMset('user:'.$uid, array('uid'=>$uid, 'name'=>$username, 'password'=>$password, 'age'=>$age));
//把用户 id 存入链表
$redis->rpush('uid', $uid);
//把用户名 name 存入字符串
$redis->set('username:'.$username, $uid); //跳转
header('location:list.php');

在 redis 客户端 flushdb 清空数据库,重新注册;

增加 login.php:

<?php
header("content-type:text/html;charset=utf-8");
require 'redis.php';
if(isset($_POST) && !empty($_POST)){
$username = $_POST['username'];
$pass = $_POST['password'];
//判断用户民是否存在
$id = $redis->get('username:'.$username);
if(!empty($id)){
//用户存在,对比密码
$password = $redis->hget('user:'.$id, 'password');
if(md5($pass) == $password){
$auth = md5(time().$username.rand());
$redis->set('auth:'.$auth, $id);
setcookie('auth', $auth, time()+86400);
header('location:list.php');
}
}
}else{
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="" method="post">
<table align="left">
<tr><td>用户名:</td><td><input type="username" name="username"></td></tr>
<tr><td>密码:</td><td><input type="password" name="password"></td></tr>
<tr><td><input type="submit" value="登录"></td><td><input type="reset" value="重置"></td></tr>
</table>
</form>
</body>
</html>
<?php }?>

修改 list.php,增加 “退出”,“欢迎+用户名”:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户信息列表</title>
</head>
<body>
<a href="add.php">返回注册页面</a>
<?php
require 'redis.php';
if(!empty($_COOKIE['auth'])){
//获取 id
$id = $redis->get('auth:'.$_COOKIE['auth']);
//获取用户名
$name = $redis->hget('user:'.$id, 'name');
echo '  欢迎你, ',$name;
echo '  <a href="logout.php">退出</a>';
}else{
?>
<a href="login.php">登录</a>
<?php }?>
<?php
/*分页*/
//用户总数
$count = $redis->lsize('uid');
//页大小
$pageSize = 5;
//当前页码
$page = !empty($_GET['page'])?$_GET['page']:1;
//页总数
$pageCount = ceil($count / $pageSize); /*
第 1 页:lrange 0 4
第 2 页:lrange 5 9
第 3 页:lrange 10 14
第 n 页:lrange ($page - 1) * $pageSize ($page - 1) * $pageSize + ($pageSize - 1)
*/
$ids = $redis->lrange('uid', (($page - 1) * $pageSize), (($page - 1) * $pageSize + ($pageSize - 1)));
/*
var_dump($ids);
$page = 1
array(5) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" }
*/ foreach($ids as $v){
$data[] = $redis->hgetall('user:'.$v);
}
?>
<table border="1" cellspacing="0" cellpadding="5">
<tr>
<th>uid</th>
<th>name</th>
<th>age</th>
<th>操作</th>
</tr>
<?php foreach($data as $v){ ?>
<tr>
<td><?php echo $v['uid']?></td>
<td><?php echo $v['name']?></td>
<td><?php echo $v['age']?></td>
<td><a href="del.php?uid=<?php echo $v['uid'];?>">删除</a> | <a href="mod.php?uid=<?php echo $v['uid'];?>">编辑</a></td>
</tr>
<?php } ?>
<tr><td colspan="4">
<a href="?page=<?php echo ($page-1)<=1?1:($page-1);?>">上一页</a>
<a href="?page=<?php echo ($page+1)>$pageCount?$pageCount:($page+1);?>">下一页 </a>
<a href="?page=1">首页</a>
<a href="?page=<?php echo $pageCount;?>">尾页</a>
当前<?php echo $page;?>页
总共<?php echo $pageCount;?>页
共<?php echo $count;?>用户
</td>
</tr>
</table>
</body>
</html>

  此时在 redis 客户端产看所有的 key:

127.0.0.1:6379> keys *
1) "user:4"
2) "user:1"
3) "user:6"
4) "user:3"
5) "uid"
6) "username:\xe5\x90\x89\xe5\xb8\x83\xe6\x96\xaf"
7) "username:\xe5\x88\x87\xe8\xb5\xab"
8) "auth:d3716d28fd2491c1983fc6bb4087c05b"
9) "user:5"
10) "username:\xe9\xbb\x98\xe5\xbe\xb7\xe8\x90\xa8\xe5\x85\x8b"
11) "userid"
12) "user:2"
13) "username:\xe5\xbe\xb7\xe5\xb8\x83\xe8\xa5\xbf"
14) "username:\xe5\x85\x8b\xe6\x96\xaf\xe5\x88\x87\xe5\xb0\x94\xe5\xb0\xbc"
15) "username:\xe5\x8a\xa0\xe5\xb8\x83\xe9\x87\x8c\xe5\x9f\x83\xe5\xb0\x94"

用户列表前台:

redis分页摘抄

退出

logout.php:

1 <?php
2 setcookie('auth', '', time()-1);
3 header('location:list.php');

加关注

修改 list.php(列表界面 line:64)

redis分页摘抄
 1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <title>用户信息列表</title>
6 </head>
7 <body>
8 <a href="add.php">返回注册页面</a>
9 <?php
10 require 'redis.php';
11 if(!empty($_COOKIE['auth'])){
12 //获取 id
13 $id = $redis->get('auth:'.$_COOKIE['auth']);
14 //获取用户名
15 $name = $redis->hget('user:'.$id, 'name');
16 echo '&nbsp;&nbsp;欢迎你, ',$name;
17 echo '&nbsp;&nbsp;<a href="logout.php">退出</a>';
18 }else{
19 ?>
20 <a href="login.php">登录</a>
21 <?php }?>
22 <?php
23 /*分页*/
24 //用户总数
25 $count = $redis->lsize('uid');
26 //页大小
27 $pageSize = 5;
28 //当前页码
29 $page = !empty($_GET['page'])?$_GET['page']:1;
30 //页总数
31 $pageCount = ceil($count / $pageSize);
32
33 /*
34 第 1 页:lrange 0 4
35 第 2 页:lrange 5 9
36 第 3 页:lrange 10 14
37 第 n 页:lrange ($page - 1) * $pageSize ($page - 1) * $pageSize + ($pageSize - 1)
38 */
39 $ids = $redis->lrange('uid', (($page - 1) * $pageSize), (($page - 1) * $pageSize + ($pageSize - 1)));
40 /*
41 var_dump($ids);
42 $page = 1
43 array(5) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" }
44 */
45
46 foreach($ids as $v){
47 $data[] = $redis->hgetall('user:'.$v);
48 }
49 ?>
50 <table border="1" cellspacing="0" cellpadding="5">
51 <tr>
52 <th>uid</th>
53 <th>name</th>
54 <th>age</th>
55 <th>操作</th>
56 </tr>
57 <?php foreach($data as $v){ ?>
58 <tr>
59 <td><?php echo $v['uid']?></td>
60 <td><?php echo $v['name']?></td>
61 <td><?php echo $v['age']?></td>
62 <td align="center"><a href="del.php?uid=<?php echo $v['uid'];?>">删除</a>
63 | <a href="mod.php?uid=<?php echo $v['uid'];?>">编辑</a>
64 <!-- 用户登录,列表中除自己之外的用户 都显示“加关注”-->
65 <?php if(!empty($_COOKIE['auth']) && $id != $v['uid']){ ?>| <a href="addAttention.php?id=<?php echo $v['uid'];?>&uid=<?php echo $id;?>">加关注</a><?php }?></td>
66 </tr>
67 <?php } ?>
68 <tr><td colspan="4">
69 <a href="?page=<?php echo ($page-1)<=1?1:($page-1);?>">上一页</a>
70 <a href="?page=<?php echo ($page+1)>$pageCount?$pageCount:($page+1);?>">下一页 </a>
71 <a href="?page=1">首页</a>
72 <a href="?page=<?php echo $pageCount;?>">尾页</a>
73 当前<?php echo $page;?>页
74 总共<?php echo $pageCount;?>页
75 共<?php echo $count;?>用户
76 </td>
77 </tr>
78 </table>
79 </body>
80 </html>
redis分页摘抄

加关注用集合(set,有并集、交集、差集——可以实现共同关注、好友推荐(a 关注了,但是 b 没有关注,a 给 b 的好友推荐))来存储

addAttenation.php:

redis分页摘抄
 1 <?php
2 require 'redis.php';
3
4 //待加关注的用户id
5 $id = $_GET['id'];
6 //用户id
7 $uid = $_GET['uid'];
8
9 //当前用户 uid 正在following(关注)id
10 $redis->sadd('user:'.$uid.':following', $id);
11 //用户id的followers(粉丝):uid
12 $redis->sadd('user:'.$id.':followers', $uid);
13
14 header('location:list.php');
redis分页摘抄

添加完关注以后,在 redis 客户端:

redis分页摘抄
127.0.0.1:6379> keys *
1) "user:4"
2) "user:1:followers"
3) "user:3"
4) "user:6"
5) "auth:a6ce443729f3d524f81c2bdcdba60278"
6) "uid"
7) "auth:556734b6f86b074b921349d616393c2e"
8) "username:\xe5\x88\x87\xe8\xb5\xab"
9) "user:2:followers"
10) "user:2"
11) "userid"
12) "user:1:following"
13) "auth:28e59ffccd991262bfb1b92c027a69ea"
14) "username:\xe5\xbe\xb7\xe5\xb8\x83\xe8\xa5\xbf"
15) "user:1"
16) "user:5:followers"
17) "username:\xe5\x90\x89\xe5\xb8\x83\xe6\x96\xaf"
18) "user:3:followers"
19) "user:5"
20) "auth:d3716d28fd2491c1983fc6bb4087c05b"
21) "username:\xe9\xbb\x98\xe5\xbe\xb7\xe8\x90\xa8\xe5\x85\x8b"
22) "auth:a2a03ba9c65d1716d7f19726372f83f7"
23) "user:2:following"
24) "user:4:followers"
25) "auth:8ae6684aecf41ee7259a288aaecca964"
26) "username:\xe5\x8a\xa0\xe5\xb8\x83\xe9\x87\x8c\xe5\x9f\x83\xe5\xb0\x94"
27) "username:\xe5\x85\x8b\xe6\x96\xaf\xe5\x88\x87\xe5\xb0\x94\xe5\xb0\xbc"
127.0.0.1:6379> smembers user:1:following
1) "2"
2) "4"
3) "5"
127.0.0.1:6379> smembers user:1:followers
1) "2"
127.0.0.1:6379> smembers user:2:following
1) "1"
2) "3"
3) "4"
127.0.0.1:6379> smembers user:2:followers
1) "1"
127.0.0.1:6379> sdiff user:1:following user:2:following #user:1 和 user:2 关注的差集,可以user:1 可以向 user:2 推荐好友
1) "2"
2) "5"
redis分页摘抄

我的关注

修改 list.php(line:79):

redis分页摘抄
 1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <title>用户信息列表</title>
6 </head>
7 <body>
8 <a href="add.php">返回注册页面</a>
9 <?php
10 require 'redis.php';
11 if(!empty($_COOKIE['auth'])){
12 //获取 id
13 $id = $redis->get('auth:'.$_COOKIE['auth']);
14 //获取用户名
15 $name = $redis->hget('user:'.$id, 'name');
16 echo '&nbsp;&nbsp;欢迎你, ',$name;
17 echo '&nbsp;&nbsp;<a href="logout.php">退出</a>';
18 }else{
19 ?>
20 <a href="login.php">登录</a>
21 <?php }?>
22 <?php
23 /*分页*/
24 //用户总数
25 $count = $redis->lsize('uid');
26 //页大小
27 $pageSize = 5;
28 //当前页码
29 $page = !empty($_GET['page'])?$_GET['page']:1;
30 //页总数
31 $pageCount = ceil($count / $pageSize);
32
33 /*
34 第 1 页:lrange 0 4
35 第 2 页:lrange 5 9
36 第 3 页:lrange 10 14
37 第 n 页:lrange ($page - 1) * $pageSize ($page - 1) * $pageSize + ($pageSize - 1)
38 */
39 $ids = $redis->lrange('uid', (($page - 1) * $pageSize), (($page - 1) * $pageSize + ($pageSize - 1)));
40 /*
41 var_dump($ids);
42 $page = 1
43 array(5) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" }
44 */
45
46 foreach($ids as $v){
47 $data[] = $redis->hgetall('user:'.$v);
48 }
49 ?>
50 <table border="1" cellspacing="0" cellpadding="5">
51 <tr>
52 <th>uid</th>
53 <th>name</th>
54 <th>age</th>
55 <th>操作</th>
56 </tr>
57 <?php foreach($data as $v){ ?>
58 <tr>
59 <td><?php echo $v['uid']?></td>
60 <td><?php echo $v['name']?></td>
61 <td><?php echo $v['age']?></td>
62 <td align="center"><a href="del.php?uid=<?php echo $v['uid'];?>">删除</a>
63 | <a href="mod.php?uid=<?php echo $v['uid'];?>">编辑</a>
64 <!-- 用户登录,列表中除自己之外的用户 都显示“加关注”-->
65 <?php if(!empty($_COOKIE['auth']) && $id != $v['uid']){ ?>| <a href="addAttention.php?id=<?php echo $v['uid'];?>&uid=<?php echo $id;?>">加关注</a><?php }?></td>
66 </tr>
67 <?php } ?>
68 <tr><td colspan="4">
69 <a href="?page=<?php echo ($page-1)<=1?1:($page-1);?>">上一页</a>
70 <a href="?page=<?php echo ($page+1)>$pageCount?$pageCount:($page+1);?>">下一页 </a>
71 <a href="?page=1">首页</a>
72 <a href="?page=<?php echo $pageCount;?>">尾页</a>
73 当前<?php echo $page;?>页
74 总共<?php echo $pageCount;?>页
75 共<?php echo $count;?>用户
76 </td>
77 </tr>
78 </table>
79 <table cellspacing="0" cellpadding="2" border="1">
80 <caption>我的关注</caption>
81 <?php
82 $data = $redis->smembers('user:'.$id.':following');
83 foreach ($data as $value) {
84 $row = $redis->hgetall('user:'.$value);
85 ?>
86 <tr>
87 <td><?php echo $row['uid'];?></td>
88 <td><?php echo $row['name'];?></td>
89 <td><?php echo $row['age'];?></td>
90 </tr>
91 <?php
92 }
93 ?>
94 </table>
95 </body>
96 </html>
redis分页摘抄

前台显示:

redis分页摘抄

粉丝同理。

总结:

统计用户总数可以把用户 uid 以链表(list)结构存储;判断用户是否已经注册可以把用户名以字符串(string)类型存储;统计用户的关注、粉丝以及推荐好友可以使用集(set)数据类型。

上一篇:http请求的全过程


下一篇:maven生命周期理解