1212namespace Hyperf \Redis ;
1313
1414use Hyperf \Contract \ConnectionInterface ;
15+ use Hyperf \Contract \PoolInterface ;
1516use Hyperf \Contract \StdoutLoggerInterface ;
1617use Hyperf \Pool \Connection as BaseConnection ;
1718use Hyperf \Pool \Exception \ConnectionException ;
18- use Hyperf \Pool \Pool ;
1919use Psr \Container \ContainerInterface ;
2020
2121/**
@@ -25,15 +25,9 @@ class RedisConnection extends BaseConnection implements ConnectionInterface
2525{
2626 use ScanCaller;
2727
28- /**
29- * @var \Redis
30- */
31- protected $ connection ;
28+ protected \Redis |\RedisCluster |null $ connection = null ;
3229
33- /**
34- * @var array
35- */
36- protected $ config = [
30+ protected array $ config = [
3731 'host ' => 'localhost ' ,
3832 'port ' => 6379 ,
3933 'auth ' => null ,
@@ -58,11 +52,10 @@ class RedisConnection extends BaseConnection implements ConnectionInterface
5852
5953 /**
6054 * Current redis database.
61- * @var null|int
6255 */
63- protected $ database ;
56+ protected ? int $ database = null ;
6457
65- public function __construct (ContainerInterface $ container , Pool $ pool , array $ config )
58+ public function __construct (ContainerInterface $ container , PoolInterface $ pool , array $ config )
6659 {
6760 parent ::__construct ($ container , $ pool );
6861 $ this ->config = array_replace_recursive ($ this ->config , $ config );
@@ -104,18 +97,11 @@ public function reconnect(): bool
10497 $ cluster = $ this ->config ['cluster ' ]['enable ' ] ?? false ;
10598 $ sentinel = $ this ->config ['sentinel ' ]['enable ' ] ?? false ;
10699
107- $ redis = null ;
108- switch (true ) {
109- case $ cluster :
110- $ redis = $ this ->createRedisCluster ();
111- break ;
112- case $ sentinel :
113- $ redis = $ this ->createRedisSentinel ();
114- break ;
115- default :
116- $ redis = $ this ->createRedis ($ host , $ port , $ timeout );
117- break ;
118- }
100+ $ redis = match (true ) {
101+ $ cluster => $ this ->createRedisCluster (),
102+ $ sentinel => $ this ->createRedisSentinel (),
103+ default => $ this ->createRedis ($ host , $ port , $ timeout ),
104+ };
119105
120106 $ options = $ this ->config ['options ' ] ?? [];
121107
@@ -161,20 +147,20 @@ public function setDatabase(?int $database): void
161147 $ this ->database = $ database ;
162148 }
163149
164- protected function createRedisCluster ()
150+ protected function createRedisCluster (): \ RedisCluster
165151 {
166152 try {
167- $ paramaters = [];
168- $ paramaters [] = $ this ->config ['cluster ' ]['name ' ] ?? null ;
169- $ paramaters [] = $ this ->config ['cluster ' ]['seeds ' ] ?? [];
170- $ paramaters [] = $ this ->config ['timeout ' ] ?? 0.0 ;
171- $ paramaters [] = $ this ->config ['cluster ' ]['read_timeout ' ] ?? 0.0 ;
172- $ paramaters [] = $ this ->config ['cluster ' ]['persistent ' ] ?? false ;
153+ $ parameters = [];
154+ $ parameters [] = $ this ->config ['cluster ' ]['name ' ] ?? null ;
155+ $ parameters [] = $ this ->config ['cluster ' ]['seeds ' ] ?? [];
156+ $ parameters [] = $ this ->config ['timeout ' ] ?? 0.0 ;
157+ $ parameters [] = $ this ->config ['cluster ' ]['read_timeout ' ] ?? 0.0 ;
158+ $ parameters [] = $ this ->config ['cluster ' ]['persistent ' ] ?? false ;
173159 if (isset ($ this ->config ['auth ' ])) {
174- $ paramaters [] = $ this ->config ['auth ' ];
160+ $ parameters [] = $ this ->config ['auth ' ];
175161 }
176162
177- $ redis = new \RedisCluster (...$ paramaters );
163+ $ redis = new \RedisCluster (...$ parameters );
178164 } catch (\Throwable $ e ) {
179165 throw new ConnectionException ('Connection reconnect failed ' . $ e ->getMessage ());
180166 }
@@ -185,7 +171,7 @@ protected function createRedisCluster()
185171 protected function retry ($ name , $ arguments , \Throwable $ exception )
186172 {
187173 $ logger = $ this ->container ->get (StdoutLoggerInterface::class);
188- $ logger ->warning (sprintf ( 'Redis::__call failed, because ' . $ exception ->getMessage () ));
174+ $ logger ->warning ('Redis::__call failed, because ' . $ exception ->getMessage ());
189175
190176 try {
191177 $ this ->reconnect ();
@@ -198,7 +184,7 @@ protected function retry($name, $arguments, \Throwable $exception)
198184 return $ result ;
199185 }
200186
201- protected function createRedisSentinel ()
187+ protected function createRedisSentinel (): \ Redis
202188 {
203189 try {
204190 $ nodes = $ this ->config ['sentinel ' ]['nodes ' ] ?? [];
@@ -238,9 +224,8 @@ protected function createRedisSentinel()
238224 * @param string $host
239225 * @param int $port
240226 * @param float $timeout
241- * @return \Redis
242227 */
243- protected function createRedis ($ host , $ port , $ timeout )
228+ protected function createRedis ($ host , $ port , $ timeout ): \ Redis
244229 {
245230 $ redis = new \Redis ();
246231 if (! $ redis ->connect ((string ) $ host , (int ) $ port , $ timeout )) {
0 commit comments