Php +Redis 做消息队列

原创 2015年05月25日 16:15:54

php项目需要一个消息队列,最后为了简单选择了Redis List..

中文的Redis文档
http://redisdoc.com/
在Redis服务器已经启动的前提下.

1. 连接Redis

$redis = new Redis();
$redis->connect("127.0.0.1", "6379");  //php客户端设置的ip及端口  

Php +Redis 做消息队列

在Redis服务器已经启动的前提下.


1. 连接Redis

$redis = new Redis();
$redis->connect("127.0.0.1", "6379");  //php客户端设置的ip及端口  

2. Redis入队列

 $redis->lPush("GPS_LIST", data-notOrarrayOrObject);

我这里测试数组取不出来

Insert all the specified values at the head of the list stored at key. If key does not exist, it is created as empty list before performing the push operations. When key holds a value that is not a list, an error is returned.


3.Redis出队列 - 先进先出lPop 先进后出rPop

$redis->lPush(("GPS_LIST");

开始我用的这玩意,但是非阻塞的,不好使。

return – the value of the first element, or nil when key does not exist

redis> RPUSH mylist "one"
(integer) 1
redis> RPUSH mylist "two"
(integer) 2
redis> RPUSH mylist "three"
(integer) 3
redis> LPOP mylist
"one"
redis> LRANGE mylist 0 -1
1) "two"
2) "three"
  1. 阻塞出队列 brPop,blPop 同上

http://redis.io/commands/brpop

BRPOP is a blocking list pop primitive. It is the blocking version of RPOP because it blocks the connection when there are no elements to pop from any of the given lists. An element is popped from the tail of the first list that is non-empty, with the given keys being checked in the order that they are given.

BLPOP is a blocking list pop primitive. It is the blocking version of LPOP because it blocks the connection when there are no elements to pop from any of the given lists. An element is popped from the head of the first list that is non-empty, with the given keys being checked in the order that they are given.


redis->brPop("GPS_LIST", 5);
返回的是一个数组
0=> "GPS_LIST"
1=> "实际数据"

-----
Return value
Array reply: specifically:
A nil multi-bulk when no element could be popped and the timeout expired.
A two-element multi-bulk with the first element being the name of the key where an element was popped and the second element being the value of the popped element.
-----
redis> DEL list1 list2
(integer) 0
redis> RPUSH list1 a b c
(integer) 3
redis> BLPOP list1 list2 0
1) "list1"
2) "a"
  1. 查看队列长度 LLEN

Returns the length of the list stored at key. If key does not exist, it is interpreted as an empty list and 0 is returned. An error is returned when the value stored at key is not a list.

Examples
redis> LPUSH mylist "World"
(integer) 1
redis> LPUSH mylist "Hello"
(integer) 2
redis> LLEN mylist
(integer) 2
redis> 

2. Redis入队列

 $redis->lPush("GPS_LIST", data-notOrarrayOrObject);

我这里测试数组取不出来

Insert all the specified values at the head of the list stored at key. If key does not exist, it is created as empty list before performing the push operations. When key holds a value that is not a list, an error is returned.


3.Redis出队列 - 先进先出lPop 先进后出rPop

$redis->lPush(("GPS_LIST");

开始我用的这玩意,但是非阻塞的,不好使。

return – the value of the first element, or nil when key does not exist

redis> RPUSH mylist "one"
(integer) 1
redis> RPUSH mylist "two"
(integer) 2
redis> RPUSH mylist "three"
(integer) 3
redis> LPOP mylist
"one"
redis> LRANGE mylist 0 -1
1) "two"
2) "three"

4. 阻塞出队列 brPop,blPop 同上

http://redis.io/commands/brpop

BRPOP is a blocking list pop primitive. It is the blocking version of RPOP because it blocks the connection when there are no elements to pop from any of the given lists. An element is popped from the tail of the first list that is non-empty, with the given keys being checked in the order that they are given.

BLPOP is a blocking list pop primitive. It is the blocking version of LPOP because it blocks the connection when there are no elements to pop from any of the given lists. An element is popped from the head of the first list that is non-empty, with the given keys being checked in the order that they are given.


redis->brPop("GPS_LIST", 5);
返回的是一个数组
0=> "GPS_LIST"
1=> "实际数据"

-----
Return value
Array reply: specifically:
A nil multi-bulk when no element could be popped and the timeout expired.
A two-element multi-bulk with the first element being the name of the key where an element was popped and the second element being the value of the popped element.
-----
redis> DEL list1 list2
(integer) 0
redis> RPUSH list1 a b c
(integer) 3
redis> BLPOP list1 list2 0
1) "list1"
2) "a"

5. 查看队列长度 LLEN

Returns the length of the list stored at key. If key does not exist, it is interpreted as an empty list and 0 is returned. An error is returned when the value stored at key is not a list.

Examples
redis> LPUSH mylist "World"
(integer) 1
redis> LPUSH mylist "Hello"
(integer) 2
redis> LLEN mylist
(integer) 2
redis> 

php-redis队列实现抢购商品

公司业务,为了对抗高并发业务,使用redis队列机制,大家可以一起参考参考 1,用户点击某个商品,首先进入的是商品详情页面,这时候我们将商品库存进行入队列    /*****************...
  • will5451
  • will5451
  • 2017年06月14日 09:17
  • 1351

PHP用redis实现多进程队列

PHP用redis实现多进程队列
  • lqy5589731
  • lqy5589731
  • 2016年04月08日 17:10
  • 1951

php+redis+TP处理并发

前两天想自己研究PHP的并发问题,看到很多人都说用redis的队列处理并发很好,所以自己也去研究了一下,下面用实际项目记录一下自己的成果。 基本思路是所有操作用过redis的队列和集合处理并发 ...
  • q6627666
  • q6627666
  • 2017年10月27日 14:32
  • 107

PHP用redis实现多进程队列

简单的流程如下: 1, 添加队列信息 2, 队列信息储存在redis里面,我设置的是 10库为队列动作信息,9库为队列详细的信息 3, 服务端守护进程不断接收队列,再处理...
  • gb4215287
  • gb4215287
  • 2018年01月07日 18:42
  • 54

php结合redis实现高并发下的抢购、秒杀功能

抢购、秒杀是如今很常见的一个应用场景,主要需要解决的问题有两个: 1 高并发对数据库产生的压力 2 竞争状态下如何解决库存的正确减少("超卖"问题) 对于第一个问题,已经很容易想到用缓存来处理抢购,避...
  • nuli888
  • nuli888
  • 2016年07月09日 10:09
  • 39754

PHP中利用redis实现消息队列处理高并发请求

将请求存入redis 为了模拟多个用户的请求,使用一个for循环替代 //redis数据入队操作 $redis = new Redis(); $redis->connect('127.0.0.1'...
  • moliyiran
  • moliyiran
  • 2017年11月09日 21:25
  • 323

thinkphp+redis+队列

1,安装redis,根据自己的php版本安装对应的redis扩展(此步骤简单的描述一下)   1.1,安装 php_igbinary.dll,php_redis.dll扩展此处需要注意你的php...
  • weixin_29778143
  • weixin_29778143
  • 2017年02月08日 18:04
  • 4426

redis 队列操作的例子(php)

redis 队列操作的例子(php)
  • u011986449
  • u011986449
  • 2015年04月28日 20:59
  • 1881

PHP - Redis队列解决执行速度问题

今天优化了一个用Redis队列异步解决了提交数据反映时间慢点问题 整理下和大家分享下 一是怕自己忘了 二是希望可以帮助到你们 或多或少提供些思想也是好的...    先看下图,在没有优化之前一起提交五...
  • xuezhiwu001
  • xuezhiwu001
  • 2016年10月25日 23:09
  • 1469

php使用redis作为消息队列

安装redisredis不在centos官方源里面,需要手动下载EPEL(Extra Packages for Enterprise Linux)进行安装。 EPEL相当于是原来源的扩展,具体的介绍...
  • koastal
  • koastal
  • 2016年10月14日 10:26
  • 6776
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Php +Redis 做消息队列
举报原因:
原因补充:

(最多只允许输入30个字)