22
33import java .time .Duration ;
44
5+ import org .apache .commons .pool2 .impl .GenericObjectPool ;
6+ import org .apache .commons .pool2 .impl .GenericObjectPoolConfig ;
57import org .springframework .beans .factory .annotation .Autowired ;
68import org .springframework .boot .autoconfigure .data .redis .RedisProperties ;
9+ import org .springframework .boot .autoconfigure .data .redis .RedisProperties .Pool ;
710import org .springframework .boot .context .properties .ConfigurationProperties ;
811import org .springframework .context .annotation .Bean ;
912import org .springframework .context .annotation .Configuration ;
1518import io .lettuce .core .RedisURI ;
1619import io .lettuce .core .resource .ClientResources ;
1720import io .lettuce .core .resource .DefaultClientResources ;
21+ import io .lettuce .core .support .ConnectionPoolSupport ;
1822import lombok .AccessLevel ;
1923import lombok .Data ;
2024import lombok .Getter ;
@@ -34,6 +38,7 @@ public class RediSearchConfiguration {
3438 private String password ;
3539 private Duration timeout ;
3640 private boolean publishOnScheduler ;
41+ private int poolSize = 1 ;
3742
3843 @ Bean (destroyMethod = "shutdown" )
3944 ClientResources clientResources () {
@@ -61,6 +66,24 @@ StatefulRediSearchConnection<String, String> connection(RediSearchClient rediSea
6166 return rediSearchClient .connect ();
6267 }
6368
69+ @ Bean (name = "rediSearchConnectionPool" , destroyMethod = "close" )
70+ GenericObjectPool <StatefulRediSearchConnection <String , String >> rediSearchConnectionPool (
71+ RediSearchClient rediSearchClient ) {
72+ GenericObjectPool <StatefulRediSearchConnection <String , String >> pool = ConnectionPoolSupport
73+ .createGenericObjectPool (() -> rediSearchClient .connect (),
74+ new GenericObjectPoolConfig <StatefulRediSearchConnection <String , String >>());
75+ if (props .getLettuce ().getPool () != null ) {
76+ Pool properties = props .getLettuce ().getPool ();
77+ pool .setMaxTotal (properties .getMaxActive ());
78+ pool .setMaxIdle (properties .getMaxIdle ());
79+ pool .setMinIdle (properties .getMinIdle ());
80+ if (properties .getMaxWait () != null ) {
81+ pool .setMaxWaitMillis (properties .getMaxWait ().toMillis ());
82+ }
83+ }
84+ return pool ;
85+ }
86+
6487 private String host () {
6588 if (host == null ) {
6689 return props .getHost ();
0 commit comments