PHP + Redis 队列实战

时间:2021-01-05 12:19:32

环境

centos6.5

PHP5.3

Redis安装

#yum install redis

1.redis配置认证密码

#vi /etc/redis.conf

requirepass mypass

#service redis restart

2.命令行

#redis-cli -h 127.0.0.1 -p 6379 -a mypass

>keys *

>select 1

3.常用命令

>flushall

>set key value

>get key

>keys *

>keys cg*

PHP redis扩展

yum install php-redis

lPush.php

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
for($i=0; $i<100; $i++){
$json = json_encode(array('title'=>'title '.$i, 'content'=>'i am content '.$i));
$redis->lPush('list-1', $json);
}
?>

rPop.php

<?php
$redis = new Redis();
$redis->pconnect('127.0.0.1', 6379);
while (true) {
$msg = $redis->rPop('list-1');
if ($msg !== false) {
$array = json_decode($msg, true);
var_dump($array);
continue;
}
sleep(1);
}
?>

lRange.php

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$list = $redis->lRange("list-1", 0 ,5);
print_r($list);
?>

参考

https://github.com/phpredis/phpredis

交流QQ

492829339