src/Domains/Shared/RedisCache/CacheService.php line 25

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Domains\Shared\RedisCache;
  4. use Predis\Client;
  5. class CacheService
  6. {
  7.     public function __construct(
  8.         private readonly Client $redis
  9.     ) {
  10.     }
  11.     public function set(string $key, array $dataint $ttl 3600): void
  12.     {
  13.         $this->redis->setex(
  14.             $key,
  15.             $ttl,
  16.             json_encode($data)
  17.         );
  18.     }
  19.     public function get(string $key): ?array
  20.     {
  21.         $cached $this->redis->get($key);
  22.         if ($cached === null) {
  23.             return null;
  24.         }
  25.         return json_decode($cachedtrue);
  26.     }
  27.     public function delete(string $key): void
  28.     {
  29.         $this->redis->del($key);
  30.     }
  31.     public function exists(string $key): bool
  32.     {
  33.         return (bool) $this->redis->exists($key);
  34.     }
  35. }