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.