This repository has been archived by the owner on Nov 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Add - Add `RedisCache`
- Loading branch information
문정기
committed
Feb 21, 2020
1 parent
43700c6
commit 55ac2c9
Showing
3 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Ridibooks\Platform\Common\Cache; | ||
|
||
use Predis\Client; | ||
|
||
class RedisCache | ||
{ | ||
/** @var Client|null */ | ||
private $client; | ||
|
||
public function __construct(string $host, int $port = 6379) | ||
{ | ||
try { | ||
$this->client = new Client(['scheme' => 'tcp', 'host' => $host, 'port' => $port,]); | ||
} catch (\Exception $e) { | ||
$this->client = null; | ||
trigger_error($e->getMessage()); | ||
} | ||
} | ||
|
||
public function get(string $key): ?string | ||
{ | ||
try { | ||
if ($this->client !== null) { | ||
return $this->client->get($key); | ||
} | ||
} catch (\Exception $e) { | ||
trigger_error($e->getMessage()); | ||
if ($this->client !== null && !$this->client->isConnected()) { | ||
$this->client->connect(); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function setJson(string $key, array $value, int $ttl): void | ||
{ | ||
$this->set($key, json_encode($value), $ttl); | ||
} | ||
|
||
public function set(string $key, string $value, int $ttl): void | ||
{ | ||
try { | ||
if ($this->client !== null) { | ||
// setnx()은 호출 시점에서 해당하는 key-value가 존재하지 않는 경우에만 set이 성공한다. | ||
// set 성공 시 return 1, 실패 시 return 0 | ||
$result = $this->client->setnx($key, $value); | ||
if ($result === 1) { | ||
$this->client->expire($key, $ttl); | ||
} | ||
} | ||
} catch (\Exception $e) { | ||
trigger_error($e->getMessage()); | ||
if ($this->client !== null && !$this->client->isConnected()) { | ||
$this->client->connect(); | ||
} | ||
} | ||
} | ||
} |