一.redis.php
Php代码:
//实例化
$redis=newRedis();
//连接服务器
$redis->connect("127.0.0.1");
//授权
$redis->auth("haitao");
//相关操作
$redis->set("name","haitao");
$data=$redis->key("*");
var_dump($data);
二.简单回顾redis的四种数据类型
a.string:最简单的数据类型
Unix代码
set user:001:name lijie
set user:001:age20
b.hash:可以当做表,hash table,比string速度快
Unix代码
hset user:001name lamp age20
hset user:001sex nan
hset user:002name lijie age20
hgetall user:001
c.list:栈、队列
d.set:并集、交集、差集
e.zset:set升级版,多了一个顺序
三.小型的用户管理系统(用户的增删改查、分页、登陆退出、加关注)
redis.php
//实例化
$redis=newRedis();
//连接服务器
$redis->connect("localhost");
//授权
$redis->auth("lamplijie");
add.php
reg.php
require("redis.php");
$username=$_POST['username'];
$password= md5($_POST['password']);
$age=$_POST['age'];
echo$uid=$redis->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
del.php
require("redis.php");
$uid=$_GET['id'];
$redis->del("user:".$uid);
$redis->lrem("uid",$uid);
header("localhost:list.php");
mod.php
doedit.php
$uid=$_POST['uid'];
$username=$_POST['username'];
$age=$_POST['age'];
$a=$redis->hmset("user:".$uid,array("username"=>$username,"age"=>$age));
if($a) {
header("location:list.php");
}else{
header(location:mod.php?id=".$uid);
}
login.php
logout.php
setcookie("auth","",time()-1);
header("location:list.php");
addfans.php
$id=$_GET['id'];
$uid=$_GET['uid'];
require("redis.php");
$redis->sadd("user:".$uid.":following",$id);
$redis->sadd("user:".$id.":followers",$uid);
header("location:list.php");