redis+php实战之小型的用户管理系统

一.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");


当然,采用sdiff user:1:following user:2:following语句,用户1可以向用户2推荐关注(即用户1的关注与用户2的关注的差集)。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容