Skip to content
This repository was archived by the owner on May 19, 2022. It is now read-only.

Commit ebf9343

Browse files
committed
added bean lifecycle handling
1 parent d89856a commit ebf9343

File tree

1 file changed

+53
-9
lines changed

1 file changed

+53
-9
lines changed

src/main/java/com/redislabs/springredisearch/RediSearchConfiguration.java

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package com.redislabs.springredisearch;
22

3+
import java.time.Duration;
4+
35
import org.springframework.beans.factory.annotation.Autowired;
46
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
57
import org.springframework.boot.context.properties.ConfigurationProperties;
68
import org.springframework.context.annotation.Bean;
79
import org.springframework.context.annotation.Configuration;
810

911
import com.redislabs.lettusearch.RediSearchClient;
12+
import com.redislabs.lettusearch.StatefulRediSearchConnection;
1013

1114
import io.lettuce.core.RedisURI;
15+
import io.lettuce.core.resource.ClientResources;
16+
import io.lettuce.core.resource.DefaultClientResources;
1217
import lombok.AccessLevel;
1318
import lombok.Data;
1419
import 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

Comments
 (0)