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