Php +Redis 做訊息佇列

來源:互聯網
上載者:User

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> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.