Files
oficial/app/src/Service/Redis.php

37 lines
941 B
PHP
Raw Normal View History

2023-10-19 20:46:52 -03:00
<?php
namespace Incoviba\Service;
2024-04-02 13:50:08 -03:00
use Predis\ClientInterface;
2023-10-19 21:49:57 -03:00
use Incoviba\Common\Implement\Exception\EmptyRedis;
2025-02-03 22:17:57 -03:00
use Predis\Connection\ConnectionException;
2023-10-19 20:46:52 -03:00
class Redis
{
2024-04-02 13:50:08 -03:00
public function __construct(protected ClientInterface $client) {}
2023-10-19 20:46:52 -03:00
2025-03-03 14:57:22 -03:00
/**
* @param string $name
* @return string|null
* @throws EmptyRedis
*/
public function get(string $name): ?string
2023-10-19 20:46:52 -03:00
{
2025-02-03 22:17:57 -03:00
try {
if (!$this->client->exists($name)) {
throw new EmptyRedis($name);
}
return $this->client->get($name);
} catch (ConnectionException $exception) {
throw new EmptyRedis($name, $exception);
2023-10-19 21:49:57 -03:00
}
2023-10-19 20:46:52 -03:00
}
2023-10-20 19:03:29 -03:00
public function set(string $name, mixed $value, int $expirationTTL = 60 * 60 * 24): void
2023-10-19 20:46:52 -03:00
{
2025-02-03 22:17:57 -03:00
try {
$this->client->set($name, $value, 'EX', $expirationTTL);
} catch (ConnectionException) {
return;
}
2023-10-19 20:46:52 -03:00
}
}