-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
482 additions
and
168 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of Swoft. | ||
* | ||
* @link https://swoft.org | ||
* @document https://swoft.org/docs | ||
* @contact [email protected] | ||
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Swoft\Redis; | ||
|
||
|
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 |
---|---|---|
@@ -1,4 +1,12 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* This file is part of Swoft. | ||
* | ||
* @link https://swoft.org | ||
* @document https://swoft.org/docs | ||
* @contact [email protected] | ||
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Swoft\Redis\Connection; | ||
|
||
|
@@ -265,7 +273,7 @@ public function command(string $method, array $parameters = [], bool $reconnect | |
$this->release(); | ||
} catch (Throwable $e) { | ||
if (!$reconnect && $this->reconnect()) { | ||
\vdump($e->getMessage(), $this->client->getLastError()); | ||
vdump($e->getMessage(), $this->client->getLastError()); | ||
return $this->command($method, $parameters, true); | ||
} | ||
|
||
|
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 |
---|---|---|
@@ -1,4 +1,12 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* This file is part of Swoft. | ||
* | ||
* @link https://swoft.org | ||
* @document https://swoft.org/docs | ||
* @contact [email protected] | ||
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Swoft\Redis\Connection; | ||
|
||
|
@@ -67,5 +75,4 @@ public function release(bool $final = false): void | |
$this->unset($finalKey); | ||
} | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -1,15 +1,20 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of Swoft. | ||
* | ||
* @link https://swoft.org | ||
* @document https://swoft.org/docs | ||
* @contact [email protected] | ||
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Swoft\Redis\Connection; | ||
|
||
|
||
/** | ||
* Class PhpRedisClusterConnection | ||
* | ||
* @since 2.0 | ||
*/ | ||
class PhpRedisClusterConnection extends Connection | ||
{ | ||
|
||
} |
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 |
---|---|---|
@@ -1,5 +1,12 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of Swoft. | ||
* | ||
* @link https://swoft.org | ||
* @document https://swoft.org/docs | ||
* @contact [email protected] | ||
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Swoft\Redis\Connection; | ||
|
||
|
@@ -14,5 +21,4 @@ | |
*/ | ||
class PhpRedisConnection extends Connection | ||
{ | ||
|
||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,12 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of Swoft. | ||
* | ||
* @link https://swoft.org | ||
* @document https://swoft.org/docs | ||
* @contact [email protected] | ||
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Swoft\Redis\Connector; | ||
|
||
|
@@ -37,29 +44,66 @@ public function connect(array $config, array $option): Redis | |
$client->auth($config['password']); | ||
} | ||
|
||
if (!empty($config['database'])) { | ||
if (isset($config['database'])) { | ||
$client->select($config['database']); | ||
} | ||
|
||
if (isset($config['read_timeout'])) { | ||
$client->setOption(Redis::OPT_READ_TIMEOUT, (int)$config['read_timeout']); | ||
// - read_timeout: float, value in seconds (optional, default is 0 meaning unlimited) | ||
if (!empty($config['read_timeout'])) { | ||
$client->setOption(Redis::OPT_READ_TIMEOUT, (float)$config['read_timeout']); | ||
} | ||
|
||
if (!empty($option['prefix'])) { | ||
$client->setOption(Redis::OPT_PREFIX, $option['prefix']); | ||
} | ||
|
||
if (isset($option['serializer'])) { | ||
if (!empty($option['serializer'])) { | ||
$client->setOption(Redis::OPT_SERIALIZER, (int)$option['serializer']); | ||
} | ||
|
||
if (!empty($option['tcp_keepalive'])) { | ||
$client->setOption(Redis::OPT_TCP_KEEPALIVE, $option['tcp_keepalive']); | ||
} | ||
|
||
if (isset($option['scan'])) { | ||
$client->setOption(Redis::OPT_SCAN, (int)$option['scan']); | ||
} | ||
|
||
return $client; | ||
} | ||
|
||
/** | ||
* Establish a connection with the Redis host. | ||
* | ||
* @param Redis $client | ||
* @param array $config | ||
* | ||
* @return void | ||
* @throws RedisException | ||
*/ | ||
protected function establishConnection(Redis $client, array $config): void | ||
{ | ||
$parameters = [ | ||
$config['host'], | ||
$config['port'], | ||
(float)$config['timeout'], // timeout: float, value in seconds (optional, default is 0 meaning unlimited) | ||
'', | ||
$config['retry_interval'], | ||
]; | ||
|
||
// - read_timeout: float, value in seconds (optional, default is 0 meaning unlimited) | ||
if (version_compare(phpversion('redis'), '3.1.3', '>=')) { | ||
$parameters[] = (float)$config['read_timeout']; | ||
} | ||
|
||
$result = $client->connect(...$parameters); | ||
if ($result === false) { | ||
throw new RedisException( | ||
sprintf('Redis connect error(%s)', JsonHelper::encode($parameters, JSON_UNESCAPED_UNICODE)) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @param array $config | ||
* @param array $option | ||
|
@@ -71,13 +115,15 @@ public function connect(array $config, array $option): Redis | |
*/ | ||
public function connectToCluster(array $config, array $option): RedisCluster | ||
{ | ||
$servers = array_map([$this, 'buildClusterConnectionString'], $config); | ||
$servers = array_values($servers); // Create a cluster setting two nodes as seeds | ||
$servers = array_map([$this, 'buildClusterConnectionString'], $config); | ||
$servers = array_values($servers); // Create a cluster setting two nodes as seeds | ||
|
||
$readTimeout = $option['read_timeout'] ?? 0; | ||
$timeout = $option['timeout'] ?? 0; | ||
$persistent = $option['persistent'] ?? false; // persistent connections to each node | ||
$name = $option['name'] ?? null; | ||
$auth = $option['auth'] ?? ''; // Connect with cluster using password. | ||
|
||
$timeout = $option['timeout'] ?? 0; | ||
$name = $option['name'] ?? null; | ||
$auth = $option['auth'] ?? ''; // Connect with cluster using password. | ||
|
||
$parameters = compact('name', 'servers', 'timeout', 'readTimeout', 'persistent'); | ||
$parameters = array_values($parameters); | ||
|
@@ -105,8 +151,8 @@ protected function setRedisClusterOptions(RedisCluster $redisCluster, array $opt | |
$redisCluster->setOption(RedisCluster::OPT_PREFIX, $option['prefix']); | ||
} | ||
|
||
if (!empty($option['serializer'])) { | ||
$redisCluster->setOption(RedisCluster::OPT_SERIALIZER, (string)$option['serializer']); | ||
if (isset($option['serializer'])) { | ||
$redisCluster->setOption(RedisCluster::OPT_SERIALIZER, (int)$option['serializer']); | ||
} | ||
|
||
if (!empty($option['scan'])) { | ||
|
@@ -143,35 +189,4 @@ protected function buildClusterConnectionString(array $server): string | |
|
||
return $base; | ||
} | ||
|
||
/** | ||
* Establish a connection with the Redis host. | ||
* | ||
* @param Redis $client | ||
* @param array $config | ||
* | ||
* @return void | ||
* @throws RedisException | ||
*/ | ||
protected function establishConnection(Redis $client, array $config): void | ||
{ | ||
$parameters = [ | ||
$config['host'], | ||
$config['port'], | ||
$config['timeout'], | ||
'', | ||
$config['retry_interval'], | ||
]; | ||
|
||
if (version_compare(phpversion('redis'), '3.1.3', '>=')) { | ||
$parameters[] = $config['read_timeout']; | ||
} | ||
|
||
$result = $client->connect(...$parameters); | ||
if ($result === false) { | ||
throw new RedisException( | ||
sprintf('Redis connect error(%s)', JsonHelper::encode($parameters, JSON_UNESCAPED_UNICODE)) | ||
); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,12 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of Swoft. | ||
* | ||
* @link https://swoft.org | ||
* @document https://swoft.org/docs | ||
* @contact [email protected] | ||
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Swoft\Redis\Contract; | ||
|
||
|
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 |
---|---|---|
@@ -1,5 +1,12 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of Swoft. | ||
* | ||
* @link https://swoft.org | ||
* @document https://swoft.org/docs | ||
* @contact [email protected] | ||
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Swoft\Redis\Contract; | ||
|
||
|
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 |
---|---|---|
@@ -1,4 +1,12 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* This file is part of Swoft. | ||
* | ||
* @link https://swoft.org | ||
* @document https://swoft.org/docs | ||
* @contact [email protected] | ||
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Swoft\Redis\Exception; | ||
|
||
|
@@ -11,5 +19,4 @@ | |
*/ | ||
class RedisException extends RuntimeException | ||
{ | ||
|
||
} |
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 |
---|---|---|
@@ -1,4 +1,12 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* This file is part of Swoft. | ||
* | ||
* @link https://swoft.org | ||
* @document https://swoft.org/docs | ||
* @contact [email protected] | ||
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Swoft\Redis\Listener; | ||
|
||
|
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 |
---|---|---|
@@ -1,4 +1,12 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* This file is part of Swoft. | ||
* | ||
* @link https://swoft.org | ||
* @document https://swoft.org/docs | ||
* @contact [email protected] | ||
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Swoft\Redis\Listener; | ||
|
||
|
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 |
---|---|---|
@@ -1,5 +1,12 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of Swoft. | ||
* | ||
* @link https://swoft.org | ||
* @document https://swoft.org/docs | ||
* @contact [email protected] | ||
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Swoft\Redis\Listener; | ||
|
||
|
@@ -34,4 +41,4 @@ public function handle(EventInterface $event): void | |
$pool->initPool(); | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,12 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* This file is part of Swoft. | ||
* | ||
* @link https://swoft.org | ||
* @document https://swoft.org/docs | ||
* @contact [email protected] | ||
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Swoft\Redis\Listener; | ||
|
||
|
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 |
---|---|---|
@@ -1,5 +1,12 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of Swoft. | ||
* | ||
* @link https://swoft.org | ||
* @document https://swoft.org/docs | ||
* @contact [email protected] | ||
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Swoft\Redis; | ||
|
||
|
Oops, something went wrong.