Redis 笔记与总结8 PHP + Redis 信息管理系统(分页+好友关注)
分页
要对列表页进行分页,需要知道:
①用户总数 $count
② 页大小 $pageSize:用户自定义
③ 当前页:$page:GET 方式获取
④ 总页数:$pageCount = ceil($count / $pageSize)
关键是用户总数 $count 的获取:
可以采取的方案是,在用户注册时,把 uid 存入链表,统计链表中用户 uid 的个数,即为用户总数 $count。
【操作】
reg.php:(line 14)
1 <?php 2 require 'redis.php'; 3 4 $username = $_POST['username']; 5 $password = md5($_POST['password']); 6 $age =$_POST['age']; 7 //uid 自加 8 //当有 userid 这个键时,执行 incr时该键的值加1;不存在该键时,创建一个 userid,值为0,执行incr,值为1 9 $uid = $redis->incr('userid'); 10 11 //向 hash 表中批量添加数据:hMset 12 $redis->hMset('user:'.$uid, array('uid'=>$uid, 'name'=>$username, 'password'=>$password, 'age'=>$age)); 13 //把用户 id 存入链表 14 $redis->rpush('uid', $uid); 15 16 //跳转 17 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"
前台:
删除用户时,同时删除链表结构中的对应的用户的 uid :
del.php(line:7):
1 <?php 2 require 'redis.php'; 3 4 $uid = $_GET['uid']; 5 $redis->del('user:'.$uid); 6 //删除链表中的用户 uid 7 $redis->lrem('uid', $uid); 8 9 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):
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 <a href="login.php">登录</a> 10 <?php 11 require 'redis.php'; 12 13 /*分页*/ 14 //用户总数 15 $count = $redis->lsize('uid'); 16 //页大小 17 $pageSize = 5; 18 //当前页码 19 $page = !empty($_GET['page'])?$_GET['page']:1; 20 //页总数 21 $pageCount = ceil($count / $pageSize); 22 23 /* 24 第 1 页:lrange 0 4 25 第 2 页:lrange 5 9 26 第 3 页:lrange 10 14 27 第 n 页:lrange ($page - 1) * $pageSize ($page - 1) * $pageSize + ($pageSize - 1) 28 */ 29 $ids = $redis->lrange('uid', (($page - 1) * $pageSize), (($page - 1) * $pageSize + ($pageSize - 1))); 30 /* 31 var_dump($ids); 32 $page = 1 33 array(5) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" } 34 */ 35 36 foreach($ids as $v){ 37 $data[] = $redis->hgetall('user:'.$v); 38 } 39 ?> 40 <table border="1" cellspacing="0" cellpadding="5"> 41 <tr> 42 <th>uid</th> 43 <th>name</th> 44 <th>age</th> 45 <th>操作</th> 46 </tr> 47 <?php foreach($data as $v){ ?> 48 <tr> 49 <td><?php echo $v['uid']?></td> 50 <td><?php echo $v['name']?></td> 51 <td><?php echo $v['age']?></td> 52 <td><a href="del.php?uid=<?php echo $v['uid'];?>">删除</a> | <a href="mod.php?uid=<?php echo $v['uid'];?>">编辑</a></td> 53 </tr> 54 <?php } ?> 55 <tr><td colspan="4"> 56 <a href="?page=<?php echo ($page-1)<=1?1:($page-1);?>">上一页</a> 57 <a href="?page=<?php echo ($page+1)>$pageCount?$pageCount:($page+1);?>">下一页 </a> 58 <a href="?page=1">首页</a> 59 <a href="?page=<?php echo $pageCount;?>">尾页</a> 60 当前<?php echo $page;?>页 61 总共<?php echo $pageCount;?>页 62 共<?php echo $count;?>用户 63 </td> 64 </tr> 65 </table> 66 </body> 67 </html>
前台:
登录
需要判断用户输入的用户名是否已经注册,实现的方法是:在用户注册的时候把用户名存入字符串,修改 reg.php(注册):
reg.php(line:15):
1 <?php 2 require 'redis.php'; 3 4 $username = $_POST['username']; 5 $password = md5($_POST['password']); 6 $age =$_POST['age']; 7 //uid 自加 8 //当有 userid 这个键时,执行 incr时该键的值加1;不存在该键时,创建一个 userid,值为0,执行incr,值为1 9 $uid = $redis->incr('userid'); 10 11 //向 hash 表中批量添加数据:hMset 12 $redis->hMset('user:'.$uid, array('uid'=>$uid, 'name'=>$username, 'password'=>$password, 'age'=>$age)); 13 //把用户 id 存入链表 14 $redis->rpush('uid', $uid); 15 //把用户名 name 存入字符串 16 $redis->set('username:'.$username, $uid); 17 18 //跳转 19 header('location:list.php');
在 redis 客户端 flushdb 清空数据库,重新注册;
增加 login.php:
1 <?php 2 header("content-type:text/html;charset=utf-8"); 3 require 'redis.php'; 4 if(isset($_POST) && !empty($_POST)){ 5 $username = $_POST['username']; 6 $pass = $_POST['password']; 7 //判断用户民是否存在 8 $id = $redis->get('username:'.$username); 9 if(!empty($id)){ 10 //用户存在,对比密码 11 $password = $redis->hget('user:'.$id, 'password'); 12 if(md5($pass) == $password){ 13 $auth = md5(time().$username.rand()); 14 $redis->set('auth:'.$auth, $id); 15 setcookie('auth', $auth, time()+86400); 16 header('location:list.php'); 17 } 18 } 19 }else{ 20 ?> 21 <!DOCTYPE html> 22 <html lang="en"> 23 <head> 24 <meta charset="UTF-8"> 25 <title>Document</title> 26 </head> 27 <body> 28 <form action="" method="post"> 29 <table align="left"> 30 <tr><td>用户名:</td><td><input type="username" name="username"></td></tr> 31 <tr><td>密码:</td><td><input type="password" name="password"></td></tr> 32 <tr><td><input type="submit" value="登录"></td><td><input type="reset" value="重置"></td></tr> 33 </table> 34 </form> 35 </body> 36 </html> 37 <?php }?>
修改 list.php,增加 “退出”,“欢迎+用户名”:
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 ' 欢迎你, ',$name; 17 echo ' <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><a href="del.php?uid=<?php echo $v['uid'];?>">删除</a> | <a href="mod.php?uid=<?php echo $v['uid'];?>">编辑</a></td> 63 </tr> 64 <?php } ?> 65 <tr><td colspan="4"> 66 <a href="?page=<?php echo ($page-1)<=1?1:($page-1);?>">上一页</a> 67 <a href="?page=<?php echo ($page+1)>$pageCount?$pageCount:($page+1);?>">下一页 </a> 68 <a href="?page=1">首页</a> 69 <a href="?page=<?php echo $pageCount;?>">尾页</a> 70 当前<?php echo $page;?>页 71 总共<?php echo $pageCount;?>页 72 共<?php echo $count;?>用户 73 </td> 74 </tr> 75 </table> 76 </body> 77 </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"
用户列表前台:
退出
logout.php:
1 <?php 2 setcookie('auth', '', time()-1); 3 header('location:list.php');
加关注
修改 list.php(列表界面 line:64)
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 ' 欢迎你, ',$name; 17 echo ' <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>
加关注用集合(set,有并集、交集、差集——可以实现共同关注、好友推荐(a 关注了,但是 b 没有关注,a 给 b 的好友推荐))来存储
addAttenation.php:
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 客户端:
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"
我的关注
修改 list.php(line:79):
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 ' 欢迎你, ',$name; 17 echo ' <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>
前台显示:
粉丝同理。
总结:
统计用户总数可以把用户 uid 以链表(list)结构存储;判断用户是否已经注册可以把用户名以字符串(string)类型存储;统计用户的关注、粉丝以及推荐好友可以使用集(set)数据类型。