Php +Redis 做消息队列

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> 
阅读更多
文章标签: redis php
个人分类: php redis
想对作者说点什么? 我来说一句

php+redis消息队列抢购实现

linlin_xia linlin_xia

2017-04-01 15:22:51

阅读数:1447

没有更多推荐了,返回首页

不良信息举报

Php +Redis 做消息队列

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭