Redis service
This commit is contained in:
24
app/src/Controller/API/withRedis.php
Normal file
24
app/src/Controller/API/withRedis.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user