11package com .redislabs .springredisearch ;
22
3+ import java .time .Duration ;
4+
35import org .springframework .beans .factory .annotation .Autowired ;
46import org .springframework .boot .autoconfigure .data .redis .RedisProperties ;
57import org .springframework .boot .context .properties .ConfigurationProperties ;
68import org .springframework .context .annotation .Bean ;
79import org .springframework .context .annotation .Configuration ;
810
911import com .redislabs .lettusearch .RediSearchClient ;
12+ import com .redislabs .lettusearch .StatefulRediSearchConnection ;
1013
1114import io .lettuce .core .RedisURI ;
15+ import io .lettuce .core .resource .ClientResources ;
16+ import io .lettuce .core .resource .DefaultClientResources ;
1217import lombok .AccessLevel ;
1318import lombok .Data ;
1419import lombok .Getter ;
@@ -26,18 +31,57 @@ public class RediSearchConfiguration {
2631 private String host ;
2732 private Integer port ;
2833 private String password ;
34+ private Duration timeout ;
35+
36+ @ Bean (destroyMethod = "shutdown" )
37+ ClientResources clientResources () {
38+ return DefaultClientResources .create ();
39+ }
40+
41+ @ Bean (destroyMethod = "shutdown" )
42+ RediSearchClient client (ClientResources clientResources ) {
43+ RedisURI redisURI = RedisURI .create (host (), port ());
44+ String password = password ();
45+ if (password != null ) {
46+ redisURI .setPassword (password );
47+ }
48+ Duration timeout = timeout ();
49+ if (timeout != null ) {
50+ redisURI .setTimeout (timeout );
51+ }
52+ return RediSearchClient .create (clientResources , redisURI );
53+ }
54+
55+ @ Bean (destroyMethod = "close" )
56+ StatefulRediSearchConnection <String , String > connection (RediSearchClient rediSearchClient ) {
57+ return rediSearchClient .connect ();
58+ }
59+
60+ private String host () {
61+ if (host == null ) {
62+ return props .getHost ();
63+ }
64+ return host ;
65+ }
2966
30- @ Bean
31- public RediSearchClient getClient () {
32- RedisURI redisURI = RedisURI .create (host == null ? props .getHost () : host ,
33- port == null ? props .getPort () : port );
34- if (password != null || props .getPassword () != null ) {
35- redisURI .setPassword (password == null ? props .getPassword () : password );
67+ private int port () {
68+ if (port == null ) {
69+ return props .getPort ();
3670 }
37- if (props .getTimeout () != null ) {
38- redisURI .setTimeout (props .getTimeout ());
71+ return port ;
72+ }
73+
74+ private String password () {
75+ if (password == null ) {
76+ return props .getPassword ();
3977 }
40- return RediSearchClient . create ( redisURI ) ;
78+ return password ;
4179 }
4280
81+ private Duration timeout () {
82+ if (timeout == null ) {
83+ return props .getTimeout ();
84+ }
85+ return timeout ;
86+ }
4387}
0 commit comments