?
Personal understanding the use of Message Queuing in a project is typically for several reasons:
Change the request processing of the instantaneous server into asynchronous processing to relieve the pressure of the server
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
$redis
=
new Redis();
$redis
->connect(
‘127.0.0.1‘
,6379);
$password
=
‘123456‘
;
$redis
->auth(
$password
);
$arr
=
array
(
‘h‘
,
‘e‘
,
‘l‘
,
‘l‘
,
‘o‘
,
‘w‘
,
‘o‘
,
‘r‘
,
‘l‘
,
‘d‘
); foreach
(
$arr
as
$k
=>
$v
){
$redis
->rpush(
"mylist"
,
$v
);
}
|
After execution, the results are as follows
?>
File: index.php timed scan out queue
12345678910111213141516171819202122232425 |
<?php
$redis
=
new
Redis();
$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