Files
oficial/app/src/Controller/withRedis.php

29 lines
831 B
PHP
Raw Normal View History

2023-10-19 20:46:52 -03:00
<?php
2023-11-23 00:53:49 -03:00
namespace Incoviba\Controller;
2023-10-19 20:46:52 -03:00
use Incoviba\Common\Implement\Exception\EmptyRedis;
use Incoviba\Service;
trait withRedis
{
public function fetchRedis(Service\Redis $redisService, string $redisKey): mixed
{
$jsonString = $redisService->get($redisKey);
if ($jsonString === null) {
throw new EmptyRedis($redisKey);
}
return json_decode($jsonString);
}
2023-11-22 19:08:19 -03:00
public function saveRedis(Service\Redis $redisService, string $redisKey, mixed $value, ?int $expiration = null): void
2023-10-19 20:46:52 -03:00
{
if (is_array($value) or is_object($value)) {
$value = json_encode($value);
}
2023-11-22 19:08:19 -03:00
if ($expiration !== null) {
$redisService->set($redisKey, $value, $expiration);
return;
}
2023-10-19 20:46:52 -03:00
$redisService->set($redisKey, $value);
}
}