一.redis.php
connect("localhost");
//授权
$redis->auth("lamplijie");
//相关操作
$redis->set("name","lampbrother");
$data = $redis->key("*");
var_dump($data);
页面显示结果:
array(1) { [0]=> string(4) "name" }
二.简单回顾redis的四种数据类型
a.string:最简单的数据类型
set user:001:name lijie set user:001:age 20
b.hash:可以当做表,hash table,比string速度快
hset user:001 name lamp age 20 hset user:001 sex nan hset user:002 name lijie age 20 hgetall user:001
c.list:栈、队列
d.set:并集、交集、差集
e.zset:set升级版,多了一个顺序
三.小型的用户管理系统(用户的增删改查、分页、登陆退出、加关注)
redis.php
connect("localhost");
//授权
$redis->auth("lamplijie");
add.php
reg.php
incr("userid");
$redis->hmset("user:".$uid,array("uid"=>$uid,"username"=>$username,"password"=>$password,"age"=>$age));
$redis->rpush("uid",$uid);
$redis->set("username:".$username,$uid);
header("location:list.php");
list.php
注册
get("auth:".$COOKIE['auth']);
$name = $redis->hget("user:".$id,"username");
?>
欢迎您,,退出
登陆
lsize("uid");
//页大小
$page_size = 3;
//当前页码
$page_num = (!empty($_GET['page']))?$_GET['page']:1;
//页总数
$page_count = ceil($count/$page_size);
$ids = $redis->lrange("uid",($page_num-1)*$page_size,(($page_num-1)*$page_size+$page_size-1));
//var_dump($ids);
/*
for($i=1;$i<=($redis->get("userid"));$i++) {
$data[] = $redis->hgetall("user:".$i);
}*/
foreach($ids as $v){
$data[] = $redis->hgetall("user:".$v);
}
//var_dump($data);
//$data = array_filter($data);
?>
uid
username
age
操作
删除
编辑
加关注
上一页
下一页
首页
尾页
当前页
总共页
总共个用户
我关注了谁
smembers("user:".$id.":following");?>
foreach($data as $v) {
$row = $redis->hgetall("user:".$v);
我的粉丝
smembers("user:".$id.":followers");
foreach($data as $v) {
$row = $redis->hgetall("user:".$v);
?>
del.php
del("user:".$uid);
$redis->lrem("uid",$uid);
header("localhost:list.php");
mod.php
hgetall("user:".$uid);
?>
doedit.php
hmset("user:".$uid,array("username"=>$username,"age"=>$age));
if($a) {
header("location:list.php");
}else {
header(location:mod.php?id=".$uid);
}
login.php
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");
}
}
?>
logout.php
addfans.php
sadd("user:".$uid.":following",$id);
$redis->sadd("user:".$id.":followers",$uid);
header("location:list.php");
当然,采用sdiff user:1:following user:2:following语句,用户1可以向用户2推荐关注(即用户1的关注与用户2的关注的差集)。