Redis-php blocking connectivity has no performance issues

Source: Internet
Author: User
I'm going to use Redis's list to do a queue system, the basic idea is:

1. Add information to the head of a list in Redis with Lpush operation
2. Write a cron schedule to execute PHP to read this list. Use Redis's rpop operation to take information from the list tail

In addition, Redis has a brpop operation that, when there is no unhandled information in the list, will block the script and continue execution when there is new information.

How can PHP Use this feature, what to pay attention to, and the other long-time connection to the performance has no impact?
PS. Is such a queuing system reasonably designed?

Reply content:

I'm going to use Redis's list to do a queue system, the basic idea is:

1. Add information to the head of a list in Redis with Lpush operation
2. Write a cron schedule to execute PHP to read this list. Use Redis's rpop operation to take information from the list tail

In addition, Redis has a brpop operation that, when there is no unhandled information in the list, will block the script and continue execution when there is new information.

How can PHP Use this feature, what to pay attention to, and the other long-time connection to the performance has no impact?
PS. Is such a queuing system reasonably designed?

First of all, you execute PHP under the shell, there is no maximum time to implement the said, you can completely use a PHP script as a process to constantly listen.

However, you do not need to use list to do the queue system, one is Brpop block is the longest time limit, you can not always hold there. And the better option is to use the pub/sub mechanism of Redis to do

Here is a simple listening process, it listens to the CHANNEL-1, you execute it under the shell do not turn off

 
  Subscribe (Array (' channel-1 '), function ($redis, $chan, $msg) {    //do something    echo $msg;});

Then send the message you need to send to channel-1 in other programs.

 
  Publish (' Channel-1 ', ' Hello, world! ');
updating the list method

If you need to use the list to operate, you can completely do not use Brpop, directly in the loop Rpop on the line

 
  Rpop (' list-1 ');    if (false!== $msg) {        //Here Processing message    }    //Here can sleep    sleep (60);}

Brpoplpush Source Destination Timeout

Brpoplpush is a blocking version of Rpoplpush, and Brpoplpush behaves as Rpoplpush when the given list source is not empty.

When the list source is empty, the Brpoplpush command blocks the connection until the wait times out, or if another client executes the Lpush or Rpush command against the source.

Timeout parameter timeout takes a number in seconds as the value. Setting the timeout parameter to 0 indicates that the blocking time can be extended indefinitely (block indefinitely).

Fully able to build a queue with list, with Brpoplpush no timeout problem

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