php+redis 实现消息队列的推送【demo】。

时间:2023-03-08 20:29:02

用redis做队列,为了缓解瞬间请求服务器的压力。实际开发当中可通过定时任务去做。当然缺点是不够实时。

1.添加一个php文件,PushQueue.php

<?php

$redis=new redis();

$redis->connect('127.0.0.1','6379');

$arr=array(1,2,3,4,5,6,7);

foreach($arr $key=>$value)

{

  $redis->lpush('mylist',$value);//redis的list集合

}

2.添加另一文件,出队列。PopQueue

//多次执行这个文件,每次会出现一个值。

<?php

$redis=new redis();

$redis->connect('127.0.0.1','6379');

echo $redis->rpop('mylist');

相关文章