Skip to content

Commit

Permalink
up: run cs fixer for redis package
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 7, 2021
1 parent 1c469f7 commit f93faf7
Show file tree
Hide file tree
Showing 31 changed files with 482 additions and 168 deletions.
7 changes: 7 additions & 0 deletions src/redis/.github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ jobs:
timeout-minutes: 20
env:
SWOFT_DEBUG: 0
services:
# https://docs.github.com/en/actions/guides/creating-redis-service-containers
redis:
image: redis
ports:
- 16379:6379 # export 16379 the port
options: --health-cmd="redis-cli ping" --health-interval=10s --health-timeout=5s --health-retries=3
strategy:
fail-fast: true # fast fail
matrix:
Expand Down
9 changes: 8 additions & 1 deletion src/redis/src/AutoLoader.php
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 group@swoft.org
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/

namespace Swoft\Redis;

Expand Down
10 changes: 9 additions & 1 deletion src/redis/src/Connection/Connection.php
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 group@swoft.org
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/

namespace Swoft\Redis\Connection;

Expand Down Expand Up @@ -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);
}

Expand Down
9 changes: 8 additions & 1 deletion src/redis/src/Connection/ConnectionManager.php
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 group@swoft.org
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/

namespace Swoft\Redis\Connection;

Expand Down Expand Up @@ -67,5 +75,4 @@ public function release(bool $final = false): void
$this->unset($finalKey);
}
}

}
11 changes: 8 additions & 3 deletions src/redis/src/Connection/PhpRedisClusterConnection.php
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 group@swoft.org
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/

namespace Swoft\Redis\Connection;


/**
* Class PhpRedisClusterConnection
*
* @since 2.0
*/
class PhpRedisClusterConnection extends Connection
{

}
12 changes: 9 additions & 3 deletions src/redis/src/Connection/PhpRedisConnection.php
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 group@swoft.org
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/

namespace Swoft\Redis\Connection;

Expand All @@ -14,5 +21,4 @@
*/
class PhpRedisConnection extends Connection
{

}
}
101 changes: 58 additions & 43 deletions src/redis/src/Connector/PhpRedisConnector.php
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 group@swoft.org
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/

namespace Swoft\Redis\Connector;

Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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'])) {
Expand Down Expand Up @@ -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))
);
}
}
}
9 changes: 8 additions & 1 deletion src/redis/src/Contract/ConnectionInterface.php
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 group@swoft.org
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/

namespace Swoft\Redis\Contract;

Expand Down
9 changes: 8 additions & 1 deletion src/redis/src/Contract/ConnectorInterface.php
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 group@swoft.org
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/

namespace Swoft\Redis\Contract;

Expand Down
9 changes: 8 additions & 1 deletion src/redis/src/Exception/RedisException.php
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 group@swoft.org
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/

namespace Swoft\Redis\Exception;

Expand All @@ -11,5 +19,4 @@
*/
class RedisException extends RuntimeException
{

}
8 changes: 8 additions & 0 deletions src/redis/src/Listener/CoroutineDeferListener.php
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 group@swoft.org
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/

namespace Swoft\Redis\Listener;

Expand Down
8 changes: 8 additions & 0 deletions src/redis/src/Listener/CoroutineDestoryListener.php
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 group@swoft.org
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/

namespace Swoft\Redis\Listener;

Expand Down
11 changes: 9 additions & 2 deletions src/redis/src/Listener/WorkerStartListener.php
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 group@swoft.org
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/

namespace Swoft\Redis\Listener;

Expand Down Expand Up @@ -34,4 +41,4 @@ public function handle(EventInterface $event): void
$pool->initPool();
}
}
}
}
8 changes: 8 additions & 0 deletions src/redis/src/Listener/WorkerStopAndErrorListener.php
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 group@swoft.org
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/

namespace Swoft\Redis\Listener;

Expand Down
9 changes: 8 additions & 1 deletion src/redis/src/Pool.php
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 group@swoft.org
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/

namespace Swoft\Redis;

Expand Down
Loading

0 comments on commit f93faf7

Please sign in to comment.