Php+redis Implementing Message Queuing

Source: Internet
Author: User
Tags message queue

?

Personal understanding the use of Message Queuing in a project is typically for several reasons:

    1. Change the request processing of the instantaneous server into asynchronous processing to relieve the pressure of the server

    2. Implement data order ordering get

The Redis implementation Message queue steps are as follows:

1). Redis function Rpush,lpop

2). Recommended scheduled tasks into the queue

3) Create a scheduled task out of queue

Files: demo.php inserting data into Redis queue

1234567891011121314151617 <?php$redisnew Redis();$redis->connect(‘127.0.0.1‘,6379);$password‘123456‘;$redis->auth($password);$arrarray(‘h‘,‘e‘,‘l‘,‘l‘,‘o‘,‘w‘,‘o‘,‘r‘,‘l‘,‘d‘); foreach($arras$k=>$v){  $redis->rpush("mylist",$v);}

  

After execution, the results are as follows

?>

File: index.php timed scan out queue

12345678910111213141516171819202122232425 <?php$redisnewRedis();$redis->connect(‘127.0.0.1‘,6379);$password‘123456‘; $redis->auth($password);//list类型出队操作$value$redis->lpop(‘mylist‘);if($value){ echo "出队的值".$value;}else{  echo"出队完成";}?>

  

Set up timed tasks

*/1 * * * * Root php/wwwroot/workplace/redis/index.php

*/3 * * * * Root php/wwwroot/workplace/redis/demo.php

Tail-f/var/log/cron viewing timed task execution

Nov 7 00:30:01 Dongzi crond[6888]: (Root) CMD (php/wwwroot/workplace/redis/demo.php)

Nov 7 00:30:01 Dongzi crond[6890]: (Root) CMD (php/wwwroot/workplace/redis/index.php)

The scheduled Task execution queue writes the following results

123456789101112131415161718192021 127.0.0.1:6379> lrange mylist 0 -1 1) "h" 2) "e" 3) "l" 4) "l" 5) "o" 6) "w" 7) "o" 8) "r" 9) "l"10) "d"

  

After the scheduled task executes out of the queue:

127.0.0.1:6379> lrange mylist 0-11) "E" 2) "L" 3) "L" 4) "O" 5) "W" 6) "O" 7) "R" 8) "L" 9) "D"

Php+redis Implementing 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.