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

25 lines
680 B
PHP
Raw Normal View History

2023-10-19 20:46:52 -03:00
<?php
namespace Incoviba\Controller\API;
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);
}
public function saveRedis(Service\Redis $redisService, string $redisKey, mixed $value): void
{
if (is_array($value) or is_object($value)) {
$value = json_encode($value);
}
$redisService->set($redisKey, $value);
}
}