Php +Redis 做消息队列

来源:互联网 发布:js 修改样式 编辑:程序博客网 时间:2024/04/19 00:41


这里的博客停止维护了  ,大家访问我的新博客: www.0352bt.com


这里的博客停止维护了  ,大家访问我的新博客: www.0352bt.com


这里的博客停止维护了  ,大家访问我的新博客: www.0352bt.com



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

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

1. 连接Redis

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

Php +Redis 做消息队列

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


1. 连接Redis

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

2. Redis入队列

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

我这里测试数组取不出来

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");
  • 1
  • 1

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

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

redis> RPUSH mylist "one"(integer) 1redis> RPUSH mylist "two"(integer) 2redis> RPUSH mylist "three"(integer) 3redis> LPOP mylist"one"redis> LRANGE mylist 0 -11) "two"2) "three"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  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 valueArray 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) 0redis> RPUSH list1 a b c(integer) 3redis> BLPOP list1 list2 01) "list1"2) "a"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  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.

Examplesredis> LPUSH mylist "World"(integer) 1redis> LPUSH mylist "Hello"(integer) 2redis> LLEN mylist(integer) 2redis> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2. Redis入队列

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

我这里测试数组取不出来

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");
  • 1
  • 1

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

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

redis> RPUSH mylist "one"(integer) 1redis> RPUSH mylist "two"(integer) 2redis> RPUSH mylist "three"(integer) 3redis> LPOP mylist"one"redis> LRANGE mylist 0 -11) "two"2) "three"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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 valueArray 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) 0redis> RPUSH list1 a b c(integer) 3redis> BLPOP list1 list2 01) "list1"2) "a"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

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.

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