Content
Test with php
Test code
<?php
count($argv) > 1 && $type = $argv[1];
if ( $type == 'subscribe' ) {
echo "subscribe\n";
$redis = new Redis();
$redis->connect('127.0.0.1', '6379');
$redis->subscribe(['order', 'storage'], function ($redis, $channel, $message) {
var_dump($redis);
if ( $channel == 'order' ) {
var_dump($channel);
print_r(json_decode($message, true));
} else {
sleep(1);
var_dump($channel);
print_r(json_decode($message, true));
}
});
$redis->close();
}elseif ( $type == 'publish' ) {
echo "publish\n";
$order = [
'id' => 1,
'name' => 'iphone 8',
'price' => 5600,
'create_time' => '2019-05-21',
];
$redis = new Redis();
$redis->connect('127.0.0.1', '6379');
for ($i = 1; $i <= 10; $i++ ) {
$order['id'] = $i;
$redis->publish("order", json_encode($order));
$storage = $order;
$storage['storage'] = 10-$i;
$redis->publish("storage", json_encode($storage));
}
$redis->close();
}else {
echo 'wrong\n';
}
Exec
Open one comand terminal window exec
$ php subscribe_publist.php subscribe
Oen another comand terminal window exec
php subscribe_publist.php publish
Back first terminal (subscribe window) info
subscribe
object(Redis)#1 (0) {
}
string(5) "order"
Array
(
[id] => 1
[name] => iphone 8
[price] => 5600
[create_time] => 2019-05-21
)
object(Redis)#1 (0) {
}
string(7) "storage"
Array
(
[id] => 1
[name] => iphone 8
[price] => 5600
[create_time] => 2019-05-21
[storage] => 2
)
object(Redis)#1 (0) {
}
string(5) "order"
Array
(
[id] => 2
[name] => iphone 8
[price] => 5600
[create_time] => 2019-05-21
)
object(Redis)#1 (0) {
}
string(7) "storage"
Array
(
[id] => 2
[name] => iphone 8
[price] => 5600
[create_time] => 2019-05-21
[storage] => 1
)
object(Redis)#1 (0) {
}
string(5) "order"
Array
(
[id] => 3
[name] => iphone 8
[price] => 5600
[create_time] => 2019-05-21
)
object(Redis)#1 (0) {
}
string(7) "storage"
Array
(
[id] => 3
[name] => iphone 8
[price] => 5600
[create_time] => 2019-05-21
[storage] => 0
)