15
15
*/
16
16
package org .springframework .data .redis .connection .lettuce ;
17
17
18
+ import static org .springframework .data .redis .connection .lettuce .LettuceConnection .*;
19
+
18
20
import io .lettuce .core .AbstractRedisClient ;
19
21
import io .lettuce .core .ClientOptions ;
20
22
import io .lettuce .core .ReadFrom ;
40
42
41
43
import org .apache .commons .logging .Log ;
42
44
import org .apache .commons .logging .LogFactory ;
45
+
43
46
import org .springframework .beans .factory .DisposableBean ;
44
47
import org .springframework .beans .factory .InitializingBean ;
45
48
import org .springframework .dao .DataAccessException ;
@@ -273,7 +276,7 @@ public void afterPropertiesSet() {
273
276
274
277
this .client = createClient ();
275
278
276
- this .connectionProvider = createConnectionProvider (client , LettuceConnection . CODEC );
279
+ this .connectionProvider = createConnectionProvider (client , CODEC );
277
280
this .reactiveConnectionProvider = createConnectionProvider (client , LettuceReactiveRedisConnection .CODEC );
278
281
279
282
if (isClusterAware ()) {
@@ -341,13 +344,7 @@ public RedisConnection getConnection() {
341
344
}
342
345
343
346
LettuceConnection connection ;
344
-
345
- if (pool != null ) {
346
- connection = new LettuceConnection (getSharedConnection (), getTimeout (), null , pool , getDatabase ());
347
- } else {
348
- connection = new LettuceConnection (getSharedConnection (), connectionProvider , getTimeout (), getDatabase ());
349
- }
350
-
347
+ connection = doCreateLettuceConnection (getSharedConnection (), connectionProvider , getTimeout (), getDatabase ());
351
348
connection .setConvertPipelineAndTxResults (convertPipelineAndTxResults );
352
349
return connection ;
353
350
}
@@ -365,12 +362,51 @@ public RedisClusterConnection getClusterConnection() {
365
362
366
363
RedisClusterClient clusterClient = (RedisClusterClient ) client ;
367
364
368
- return getShareNativeConnection ()
369
- ? new LettuceClusterConnection (
370
- (StatefulRedisClusterConnection <byte [], byte []>) getOrCreateSharedConnection ().getConnection (),
371
- connectionProvider , clusterClient , clusterCommandExecutor , clientConfiguration .getCommandTimeout ())
372
- : new LettuceClusterConnection (null , connectionProvider , clusterClient , clusterCommandExecutor ,
373
- clientConfiguration .getCommandTimeout ());
365
+ StatefulRedisClusterConnection <byte [], byte []> sharedConnection = getShareNativeConnection ()
366
+ ? (StatefulRedisClusterConnection <byte [], byte []>) getOrCreateSharedConnection ().getConnection ()
367
+ : null ;
368
+
369
+ LettuceClusterTopologyProvider topologyProvider = new LettuceClusterTopologyProvider (clusterClient );
370
+ return doCreateLettuceClusterConnection (sharedConnection , connectionProvider , topologyProvider ,
371
+ clusterCommandExecutor , clientConfiguration .getCommandTimeout ());
372
+ }
373
+
374
+ /**
375
+ * Customization hook for {@link LettuceConnection} creation.
376
+ *
377
+ * @param sharedConnection the shared {@link StatefulRedisConnection} if {@link #getShareNativeConnection()} is
378
+ * {@literal true}; {@literal null} otherwise.
379
+ * @param connectionProvider the {@link LettuceConnectionProvider} to release connections.
380
+ * @param timeout command timeout in {@link TimeUnit#MILLISECONDS}.
381
+ * @param database database index to operate on.
382
+ * @return the {@link LettuceConnection}.
383
+ * @since 2.2
384
+ */
385
+ protected LettuceConnection doCreateLettuceConnection (StatefulRedisConnection <byte [], byte []> sharedConnection ,
386
+ LettuceConnectionProvider connectionProvider , long timeout , int database ) {
387
+
388
+ return new LettuceConnection (sharedConnection , connectionProvider , timeout , database );
389
+ }
390
+
391
+ /**
392
+ * Customization hook for {@link LettuceClusterConnection} creation.
393
+ *
394
+ * @param sharedConnection the shared {@link StatefulRedisConnection} if {@link #getShareNativeConnection()} is
395
+ * {@literal true}; {@literal null} otherwise.
396
+ * @param connectionProvider the {@link LettuceConnectionProvider} to release connections.
397
+ * @param topologyProvider the {@link ClusterTopologyProvider}.
398
+ * @param clusterCommandExecutor the {@link ClusterCommandExecutor} to release connections.
399
+ * @param commandTimeout command timeout {@link Duration}.
400
+ * @return the {@link LettuceConnection}.
401
+ * @since 2.2
402
+ */
403
+ protected LettuceClusterConnection doCreateLettuceClusterConnection (
404
+ StatefulRedisClusterConnection <byte [], byte []> sharedConnection , LettuceConnectionProvider connectionProvider ,
405
+ ClusterTopologyProvider topologyProvider , ClusterCommandExecutor clusterCommandExecutor ,
406
+ Duration commandTimeout ) {
407
+
408
+ return new LettuceClusterConnection (sharedConnection , connectionProvider , topologyProvider , clusterCommandExecutor ,
409
+ commandTimeout );
374
410
}
375
411
376
412
/*
@@ -909,6 +945,10 @@ protected StatefulConnection<ByteBuffer, ByteBuffer> getSharedReactiveConnection
909
945
910
946
private LettuceConnectionProvider createConnectionProvider (AbstractRedisClient client , RedisCodec <?, ?> codec ) {
911
947
948
+ if (this .pool != null ) {
949
+ return new LettucePoolConnectionProvider (this .pool );
950
+ }
951
+
912
952
LettuceConnectionProvider connectionProvider = doCreateConnectionProvider (client , codec );
913
953
914
954
if (this .clientConfiguration instanceof LettucePoolingClientConfiguration ) {
0 commit comments