PHP implements Redis queue, waits for new message into row

Source: Internet
Author: User
Tags pconnect
1. Message into row code, t1.php:

$redis = new Redis (), $redis->connect (' 127.0.0.1 ', 6379), while (1) {        try{                $value = "Value_". Time ();                $redis->lpush (' Key1 ', $value);                echo $value. " \ n ";                Sleep (rand (1, 3));        } catch (Exception $e) {                echo $e->getmessage (). "\ n";                Exit;        }}

2. Message Dequeue Code:

$redis = new Redis (), $redis->pconnect (' 127.0.0.1 ', 6379), while (1) {        try{                $value = $redis->blpop (' Key1 ', );                if (!empty ($value)) {                        var_export ($value). " \ n ";                }        } catch (Exception $e) {                echo $e->getmessage (). "\ n";                Exit;        }}

3. What Redis uses to explain the function

1. Connect link Redis service 2. Pconnect does not actively close the link 3. Lpushlpush key value [value ...] Inserts one or more values of value into the table header of the list key. If there are multiple value values, then each value value is inserted sequentially from left to right into the header: for example, to execute Lpush mylist a b C on an empty list (mylist), the result list is C B A, which is equivalent to executing the command Lpush mylist A, Lpush MyList B, Lpush mylist c. If the key does not exist, an empty list is created and the Lpush operation is performed. An error is returned when key exists but is not a list type. Time complexity: O (1) return value: The length of the list after the Lpush command is executed. Note: Before the Redis 2.4 version of the Lpush command, only a single value value is accepted. 4.BLPOPBLPOP key [key ...] timeoutblpop is a list of blocked (blocking) pop-up primitives. It is a blocked version of the Lpop command, and the connection is blocked by the Blpop command until a wait time-out or a popup element is found when no elements in the given list are available to eject. When a number of key parameters are given, the first element of a non-empty list is ejected by checking each list in the order of key parameters.

The above introduces the implementation of the Redis queue PHP, waiting for a new message into row, including aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • 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.