Queue command with direct redis access so it's faster
This commit is contained in:
26
cli/src/Service/Job.php
Normal file
26
cli/src/Service/Job.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
namespace Incoviba\Service;
|
||||
|
||||
use Exception;
|
||||
use Predis\Connection\ConnectionException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Job
|
||||
{
|
||||
public function __construct(protected LoggerInterface $logger, protected Redis $redisService)
|
||||
{
|
||||
$this->redisKey = 'jobs';
|
||||
}
|
||||
protected string $redisKey;
|
||||
|
||||
public function getPending(): array
|
||||
{
|
||||
try {
|
||||
$jobs = $this->redisService->get($this->redisKey);
|
||||
return json_decode($jobs, true);
|
||||
} catch (ConnectionException|Exception $exception) {
|
||||
$this->logger->warning($exception);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
41
cli/src/Service/Redis.php
Normal file
41
cli/src/Service/Redis.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
namespace Incoviba\Service;
|
||||
|
||||
use Exception;
|
||||
use Predis\ClientInterface;
|
||||
use Predis\Connection\ConnectionException;
|
||||
|
||||
class Redis
|
||||
{
|
||||
public function __construct(protected ClientInterface $client) {}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return string|null
|
||||
* @throws Exception|ConnectionException
|
||||
*/
|
||||
public function get(string $name): ?string
|
||||
{
|
||||
if (!$this->client->exists($name)) {
|
||||
throw new Exception($name);
|
||||
}
|
||||
return $this->client->get($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
* @param int $expirationTTL
|
||||
* @return void
|
||||
* @throws ConnectionException
|
||||
*/
|
||||
public function set(string $name, mixed $value, int $expirationTTL = 60 * 60 * 24): void
|
||||
{
|
||||
$resolution = 'EX';
|
||||
if ($expirationTTL === -1) {
|
||||
$resolution = null;
|
||||
$expirationTTL = null;
|
||||
}
|
||||
$this->client->set($name, $value, $resolution, $expirationTTL);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user