以下是一个使用PHP实现的简单延时队列的例子。这个队列允许你将任务延迟执行,直到指定的时间。
```php

class DelayedQueue
{
private $queue = [];
private $maxId = 0;
public function add($task, $delay)
{
$this->maxId++;
$this->queue[$this->maxId] = [
'id' => $this->maxId,
'task' => $task,
'delay' => $delay,
'timestamp' => time() + $delay
];
}
public function process()
{
$currentTime = time();
foreach ($this->queue as $key => $item) {
if ($item['timestamp'] <= $currentTime) {
call_user_func($item['task']);
unset($this->queue[$key]);
}
}
}
}
// 使用示例
$delayedQueue = new DelayedQueue();
// 添加一个延时任务,5秒后执行
$delayedQueue->add(function() {
echo "







