PHP Redis Message Queuing

Source: Internet
Author: User

What Redis is not much to say, online articles a search a lot.

First of all, I want to implement the function:

Like a message broker, if someone wants to send a message, first send the message to me, and then my side of the forwarding, for is there is a unified management and modification of the convenience,

And all messages have priority, there will be timed delivery (if too many messages at the same time, there will be a delay)

Ideas:

The first one is to divide the two into two queues, one to achieve message priority, one to achieve timed delivery

The use of an ordered set of Redis, when added with Zadd, will be score than the priority, you can also use timestamps as score, to represent the time

PHP version easy to implement

Join the message to the priority queue, replace 1, 2 with the time that is scheduled to send the queue

New Redis ();  $redis->connect (' 127.0.0.1 ', 6379);  $redis->zadd (' Zset1 ', 1, ' message ');  $redis->zadd (' Zset1 ', 2, ' message2 ');     

Fetching data from the queue

$redis->zrevrangebyscore (' Zset1, ' +inf ', '-inf ', Array (' Withscores ' =>false, ' limit ' =>array (0,20)));

This statement means that from the Zset1 this queue in accordance with score from the maximum (+inf) to the smallest (-inf) of the sorting out 20, without score, if you want to grow from small to large can be used Zrangebyscore

If you want these to run under the command line, you can refer to the following, of course, these are truncated

1 <?Php2while (True) {3$pid =Pcntl_fork ();4if ($pid = =-1) {5EchoDate (' y-m-d h:i:s '). "Fork failed!" \ n ";4?Elseif ($pid = = 0) {7$redis =NewRedis (); $redis->connect (' 127.0.0.1 ', 6379);  $redis->zrevrangebyscore (' Zset1 ', ' +inf ', '-inf ') , array (' withscores ' =>false, ' limit ' =>array (0,2010 exit;11} else {12 Pcntl_ Wait ( $status }14}       

Pcntl_fork is a generated sub-process in PHP, when the function is called, a process PID is returned, and when the PID is 0 o'clock indicates that it is in the child process, so put all the things to be executed here

PHP Redis Message Queuing

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.