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) 1redis> RPUSH mylist "two"(integer) 2redis> RPUSH mylist "three"(integer) 3redis> LPOP mylist"one"redis> LRANGE mylist 0 -11) "two"2) "three"
阻塞出隊列 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"
查看隊列長度 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>
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) 1redis> RPUSH mylist "two"(integer) 2redis> RPUSH mylist "three"(integer) 3redis> LPOP mylist"one"redis> LRANGE mylist 0 -11) "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 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"
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>