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作为消息队列

安装redisredis不在centos官方源里面,需要手动下载EPEL(Extra Packages for Enterprise Linux)进行安装。 EPEL相当于是原来源的扩展,具体的介绍...
  • koastal
  • koastal
  • 2016年10月14日 10:26
  • 5458

php 实现 redis 队列,等待有新消息入列

1. 消息入列代码, t1.php: $redis = new Redis(); $redis->connect('127.0.0.1', 6379); while(1){ try...
  • wang350
  • wang350
  • 2016年01月20日 18:52
  • 1816

phpredis提高消息队列的实时性方法

phpredis提高消息队列的实时性方法 数据库存贮都用list形式 要存2个队列 1个用作消息队列保存到数据 还有个 就是用来实时读取数据在redis $redis->lpush(...

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

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

php-redis队列实现抢购商品

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

redis在PHP中的基本使用案例

利用phpredis实现php操作redis,需要进行redis.so的下载,添加//利用Redis类进行操作//实例化Redis类 $redis = new Redis(); //选择指定的redi...

redis 队列操作的例子(php)

redis 队列操作的例子(php)

基于Redis的消息队列php-resque

http://my.oschina.net/maczhao/blog/384615

用redis阻塞队列,实现消息传递

在网络服务器运行的时候,可能会碰到这样的需求,主程序处理会很耗时,            1,网络流量很大,但有紧急情况需要发送邮件通知别人,这时如果在程序中发送邮件,将会很费时,但又必须做     ...

Php +Redis 做消息队列

PHP项目需要一个消息队列,最后为了简单选择了Redis List.. 中文的Redis文档  http://redisdoc.com/  在Redis服务器已经启动的前提下. 1. 连接Redi...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Php +Redis 做消息队列
举报原因:
原因补充:

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