PHP +redis do Message Queuing

Source: Internet
Author: User
Tags message queue redis redis server

The PHP project needs a message queue, and finally to simply select the Redis List.

Redis Documents in Chinese
http://redisdoc.com/
On the premise that the Redis server has been started. 1. Connection Redis

$redis = new Redis ();
$redis->connect ("127.0.0.1", "6379");  IP and port set by PHP client  

PHP +redis do Message Queuing

On the premise that the Redis server has been started. 1. Connection Redis

$redis = new Redis ();
$redis->connect ("127.0.0.1", "6379");  IP and port set by PHP client  
2. Redis into the queue
$redis->lpush ("Gps_list", data-notorarrayorobject);

I'm testing the array here.

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 this is not a list, the error is returned. 3.Redis out Queue-advanced first out Lpop advanced Rpop

$redis->lpush ("gps_list");

I started with this thing, but it's not blocking, it doesn't work.

Return–the value of the The "the", 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 "
Block out queue Brpop,blpop ditto

Http://redis.io/commands/brpop

Brpop is a blocking list pop primitive. It is the blocking version of Rpop because it blocks the connection then there are no elements to POPs from any of the Give n lists. An element are popped from the tail of the the ' the ' is Non-empty, with the given keys being checked in the ' Order tha T they are given.

Blpop is a blocking list pop primitive. It is the blocking version of Lpop because it blocks the connection then there are no elements to POPs from any of the Give n lists. An element are popped from the ' head ' of the ' the ' is ' non-empty, with the given keys being checked in the ' Order tha T they are given.

Redis->brpop ("Gps_list", 5);
Returns an array of
0=> "gps_list"
1=> "actual data"

-----return
value
Array reply:specifically:
a nil Multi-bulk when no element could is popped and the timeout expired.
A Two-element Multi-bulk with the the ' the ' key where an element is popped and second Eleme NT being the value of the popped element.
-----
redis> DEL list1 list2
(integer) 0
redis> Rpush list1 a b C
(integer) 3
redis> BL POP list1 list2 0
1) "List1"
2 "a"
View Queue Length Llen

Returns the length of the list stored at key. If key does not exist, it is interpreted as a empty list and 0 is returned. An ' error is returned ' when the value stored at ' not a ' list.

Examples
redis> lpush mylist "World"
(integer) 1
redis> lpush mylist "Hello"
(integer) 2
Redis> llen mylist
(integer) 2
2. Redis into the queue
$redis->lpush ("Gps_list", data-notorarrayorobject);

I'm testing the array here.

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 this is not a list, the error is returned. 3.Redis out Queue-advanced first out Lpop advanced Rpop

$redis->lpush ("gps_list");

I started with this thing, but it's not blocking, it doesn't work.

Return–the value of the The "the", 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. Block out queue Brpop,blpop ditto

Http://redis.io/commands/brpop

Brpop is a blocking list pop primitive. It is the blocking version of Rpop because it blocks the connection then there are no elements to POPs from any of the Give n lists. An element are popped from the tail of the the ' the ' is Non-empty, with the given keys being checked in the ' Order tha T they are given.

Blpop is a blocking list pop primitive. It is the blocking version of Lpop because it blocks the connection then there are no elements to POPs from any of the Give n lists. An element are popped from the ' head ' of the ' the ' is ' non-empty, with the given keys being checked in the ' Order tha T they are given.

Redis->brpop ("Gps_list", 5);
Returns an array of
0=> "gps_list"
1=> "actual data"

-----return
value
Array reply:specifically:
a nil Multi-bulk when no element could is popped and the timeout expired.
A Two-element Multi-bulk with the the ' the ' key where an element is popped and second Eleme NT being the value of the popped element.
-----
redis> DEL list1 list2
(integer) 0
redis> Rpush list1 a b C
(integer) 3
redis> BL POP list1 list2 0
1) "List1"
2 "a"
5. View Queue Length Llen

Returns the length of the list stored at key. If key does not exist, it is interpreted as a empty list and 0 is returned. An ' error is returned ' when the value stored at ' not a ' list.

Examples
redis> lpush mylist "World"
(integer) 1
redis> lpush mylist "Hello"
(integer) 2
Redis> llen mylist
(integer) 2
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.