php 操作redis 佇列簡單例子
操作資料進入佇列:
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
// $password = '123456';
// $redis->auth($password);
$arr = [
['name'=>'xiaoming','age'=>20],
['name'=>'xiaoli','age'=>20],
['name'=>'sunzi','age'=>20],
['name'=>'mingwan','age'=>20],
['name'=>'lida','age'=>20],
['name'=>'kerong','age'=>20],
];
foreach($arr as $k=>$v){
$redis->rpush("mylist",json_encode($v));//加入佇列值
}
echo '佇列已經加入完成';
//操作佇列,拿出資料
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
// $password = '123456';
// $redis->auth($password);
//list型別出隊操作
$count=$redis->lSize('mylist');//獲取佇列的長度
for ($i=1; $i <=$count ; $i++) {
try{
$value = $redis->lpop('mylist');
if(!empty($value)){
var_export( json_decode($value,true ));
echo "<br/>";
}
}catch(Exception $e){
echo $e->getMessage()."<BR/>";
exit;
}
}