<?php
declare(strict_types=1);
namespace App\Domains\Shared\RedisCache;
use Predis\Client;
class CacheService
{
public function __construct(
private readonly Client $redis
) {
}
public function set(string $key, array $data, int $ttl = 3600): void
{
$this->redis->setex(
$key,
$ttl,
json_encode($data)
);
}
public function get(string $key): ?array
{
$cached = $this->redis->get($key);
if ($cached === null) {
return null;
}
return json_decode($cached, true);
}
public function delete(string $key): void
{
$this->redis->del($key);
}
public function exists(string $key): bool
{
return (bool) $this->redis->exists($key);
}
}