Skip to content

Commit

Permalink
avoid trying connection in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
mp3000mp committed Oct 11, 2020
1 parent 9652e3d commit b241046
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Usage
-----

```php
// This will create window.ListFilter
// This will try to connect and throw a RedisClientException if connection failed
$client = new RedisClient($host, $port, $auth);

// simple get set system
Expand Down
51 changes: 40 additions & 11 deletions src/RedisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ class RedisClient
*/
private $client;

private $host;

private $port;

private $auth;

/**
* RedisClient constructor.
*
Expand All @@ -28,20 +34,33 @@ public function __construct(string $host = 'localhost', int $port = 6379, ?strin
throw new RedisClientException('Redis extension missing', 1000);
}

$this->host = $host;
$this->port = $port;
$this->auth = $auth;

// try to connect
$this->client = new Redis();
try {
$this->client->connect($host, $port);
} catch (\RedisException $e) {
throw new RedisClientException($e->getMessage(), 2001);
}
if (null !== $auth) {
$this->client->auth($auth);
}
}

// test connection
/**
* @throws RedisClientException
*/
private function tryConnect()
{
if (!$this->client->isConnected()) {
throw new RedisClientException('Connection failed', 2002);
try {
$this->client->connect($this->host, $this->port);
} catch (\RedisException $e) {
throw new RedisClientException($e->getMessage(), 2001);
}
if (null !== $this->auth) {
$this->client->auth($this->auth);
}

// test connection
if (!$this->client->isConnected()) {
throw new RedisClientException('Connection failed', 2002);
}
}
}

Expand All @@ -55,6 +74,8 @@ public function __construct(string $host = 'localhost', int $port = 6379, ?strin
*/
public function get(string $key, bool $isJson = true)
{
$this->tryConnect();

$r = $this->client->get($key);

if (false !== $r && $isJson) {
Expand All @@ -75,6 +96,8 @@ public function get(string $key, bool $isJson = true)
*/
public function set(string $key, $value, ?int $timeout = null): void
{
$this->tryConnect();

if (is_array($value)) {
$value = json_encode($value);
}
Expand All @@ -91,6 +114,8 @@ public function set(string $key, $value, ?int $timeout = null): void
*/
public function getLastError(): ?string
{
$this->tryConnect();

return $this->client->getLastError();
}

Expand All @@ -99,14 +124,18 @@ public function getLastError(): ?string
*/
public function close(): void
{
$this->client->close();
if ($this->client->isConnected()) {
$this->client->close();
}
}

/**
* delete Redis key.
*/
public function delete(string $key): void
{
$this->tryConnect();

$this->client->del($key);
}
}
1 change: 1 addition & 0 deletions tests/RedisClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function testConnectionFail(): void
$this->expectException(RedisClientException::class);

$this->client = new RedisClient('unknown');
$this->client->set('no_server', 'test');
}

public function testGetSetSuccess(): void
Expand Down

0 comments on commit b241046

Please sign in to comment.