来源:免费redis的链接操作,redis的入门与应用教程-慕课网
课程只是基础介绍,详细指令见官网
常见指令都可参考文档地址:https://github.com/phpredis/phpredis
4-1 php链接redis
- Redis类的命名空间是根
因此在其他命名空间使用redis类时需要加上\
根目录符号
- phpredis的命令和参数基本和redis.io中的实际命令对应
1 2 3 4
| <?php $redis = new \Redis();
$redis->connect("127.0.0.1",7200);
|
4-2 php 操作string类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?php $redis = new \Redis(); $redis->connect('127.0.0.1',7200);
$redis->delete("string1");
$redis->set("string1","val1"); $val = $redis->get("string1"); var_dump($val);
$redis->set("string1",4); $redis->incr("string1",2); $val = $redis->get("string1"); var_dump($val);
|
4-3 php 操作list类型
list因为谁先进谁先出的特点,适合做下单
用户下单后提示用户下单成功,而后台则插入待做事项list,等实际操作后台时获取待做事项并处理。
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?php $redis = new \Redis(); $redis->connect('127.0.0.1',7200);
$redis ->delete("list1");
$redis->lpush("list1","A"); $redis->lpush("list1","B"); $redis->lpush("list1","C");
$val = $redis->rpop("list1"); var_dump($val);
|
4-4 php 操作set类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <?php $redis = new \Redis(); $redis->connect('127.0.0.1',7200);
$redis->delete("set1");
$redis->sAdd("set1","A"); $redis->sAdd("set1","B"); $redis->sAdd("set1","C"); $redis->sAdd("set1","C");
$val =$redis->sCard("set1"); var_dump($val);
$val = $redis->sMembers("set1"); var_dump($val);
|
4-4 php 操作hash类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?php $redis = new \Redis(); $redis->connect("127.0.0.1",7200);
$redis->delete("driver1");
$redis->hSet("driver1","name","mingming"); $redis->hSet("driver1","age",25); $redis->hSet("driver1","gender",1); $val = $redis->hGet("driver1","name"); var_dump($val);
$val = $redis->hMGet("driver1",array("name","age","gender")); var_dump($val);
|
4-4 php 操作sort set类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?php $redis = new \Redis(); $redis->connect('127.0.0.1',7200);
$redis->delete('zset1');
$redis->zAdd('zset1',100,'xiaoming'); $redis->zAdd('zset1',90,'xiaohong'); $redis->zAdd('zset1',93,'xiaowang');
$val = $redis->zRange("zset1",0,-1); var_dump($val);
$val = $redis->zRevRange('zset1',0,-1); var_dump($val);
|