redis 延时队列

php redis 实现延时队列

<?php
use Illuminate\Support\Facades\Redis;

Route::get('/redis', function(){

$send = 1;
$num = 1;

while ($num<10) {
$time = time() + 2 * random_int(1,9);
Redis::zadd('myzset', $time, 'user_1000'.$num); //name,score, value
$num++;
}

$mysets = Redis::ZRANGE('myzset', 0, -1, 'WITHSCORES');
dump($mysets);

while ($send<10) { //

$myset = Redis::ZRANGE('myzset', 0, 0, 'WITHSCORES');
$order = key($myset);
$orderTime = end($myset);
if(time() > $orderTime){
echo "订单=>",$order.'; 订单时间=>',date('Y-m-d H:i:s', $orderTime),PHP_EOL;
$res = Redis::zrem('myzset', $order);
dump($res, $send);
$send++;
}
}

});
tags: Redis