Files
oficial/app/src/Controller/withRedis.php
Juan Pablo Vial 7c6a397e31 CORS
2024-03-18 14:58:54 -03:00

29 lines
864 B
PHP

<?php
namespace Incoviba\Controller;
use Incoviba\Common\Implement\Exception\EmptyRedis;
use Incoviba\Service;
trait withRedis
{
public function fetchRedis(Service\Redis $redisService, string $redisKey, ?bool $asArray = null): mixed
{
$jsonString = $redisService->get($redisKey);
if ($jsonString === null) {
throw new EmptyRedis($redisKey);
}
return json_decode($jsonString, $asArray);
}
public function saveRedis(Service\Redis $redisService, string $redisKey, mixed $value, ?int $expiration = null): void
{
if (is_array($value) or is_object($value)) {
$value = json_encode($value);
}
if ($expiration !== null) {
$redisService->set($redisKey, $value, $expiration);
return;
}
$redisService->set($redisKey, $value);
}
}