hyperf框架 redis延时队列
时间:2024-8-24 22:55 作者:xiang 分类: 无
DelayedJobs
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Util;
use Hyperf\Context\ApplicationContext;
use Hyperf\Redis\Redis;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use RedisException;
class DelayedJobs
{
const KEY_RESEND="xiang_delay_queue";
/**
* @return false|int|\Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function add(string $queue_key, int $delay_seconds, array $task)
{
$container = ApplicationContext::getContainer();
$redis = $container->get(Redis::class);
$timestamp = time() + $delay_seconds;
return $redis->zAdd($queue_key, $timestamp, json_encode($task));
}
}