001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.support.redis;
017
018import io.jboot.app.config.annotation.ConfigModel;
019import io.jboot.utils.StrUtil;
020import redis.clients.jedis.HostAndPort;
021
022import java.util.HashSet;
023import java.util.Set;
024
025@ConfigModel(prefix = "jboot.redis")
026public class JbootRedisConfig {
027
028    public static final String TYPE_JEDIS = "jedis";
029    public static final String TYPE_REDISSON = "redisson";
030    public static final String TYPE_LETTUCE = "lettuce";
031
032    private String name = "default";
033    private String host;
034    private Integer port = 6379;
035    private Integer timeout = 2000;
036    private String password;
037    private Integer database = 0;
038    private String clientName;
039    private Boolean testOnCreate;
040    private Boolean testOnBorrow;
041    private Boolean testOnReturn;
042    private Boolean testWhileIdle;
043    private Long minEvictableIdleTimeMillis;
044    private Long timeBetweenEvictionRunsMillis;
045    private Integer numTestsPerEvictionRun;
046    private Integer maxAttempts;
047    private String type = TYPE_JEDIS;
048    private Integer maxTotal;
049    private Integer maxIdle;
050    private Integer minIdle;
051    private Integer maxWaitMillis;
052
053    private String serializer;
054
055    public String getName() {
056        return name;
057    }
058
059    public void setName(String name) {
060        this.name = name;
061    }
062
063    public String getHost() {
064        return host;
065    }
066
067    public void setHost(String host) {
068        this.host = host;
069    }
070
071    public Integer getPort() {
072        return port;
073    }
074
075    public void setPort(Integer port) {
076        this.port = port;
077    }
078
079    public Integer getTimeout() {
080        return timeout;
081    }
082
083    public void setTimeout(Integer timeout) {
084        this.timeout = timeout;
085    }
086
087    public String getPassword() {
088        return password;
089    }
090
091    public void setPassword(String password) {
092        this.password = password;
093    }
094
095    public Integer getDatabase() {
096        return database;
097    }
098
099    public void setDatabase(Integer database) {
100        this.database = database;
101    }
102
103    public String getClientName() {
104        return clientName;
105    }
106
107    public void setClientName(String clientName) {
108        this.clientName = clientName;
109    }
110
111    public Boolean getTestOnCreate() {
112        return testOnCreate;
113    }
114
115    public void setTestOnCreate(Boolean testOnCreate) {
116        this.testOnCreate = testOnCreate;
117    }
118
119    public Boolean getTestOnBorrow() {
120        return testOnBorrow;
121    }
122
123    public void setTestOnBorrow(Boolean testOnBorrow) {
124        this.testOnBorrow = testOnBorrow;
125    }
126
127    public Boolean getTestOnReturn() {
128        return testOnReturn;
129    }
130
131    public void setTestOnReturn(Boolean testOnReturn) {
132        this.testOnReturn = testOnReturn;
133    }
134
135    public Boolean getTestWhileIdle() {
136        return testWhileIdle;
137    }
138
139    public void setTestWhileIdle(Boolean testWhileIdle) {
140        this.testWhileIdle = testWhileIdle;
141    }
142
143    public Long getMinEvictableIdleTimeMillis() {
144        return minEvictableIdleTimeMillis;
145    }
146
147    public void setMinEvictableIdleTimeMillis(Long minEvictableIdleTimeMillis) {
148        this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
149    }
150
151    public Long getTimeBetweenEvictionRunsMillis() {
152        return timeBetweenEvictionRunsMillis;
153    }
154
155    public void setTimeBetweenEvictionRunsMillis(Long timeBetweenEvictionRunsMillis) {
156        this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
157    }
158
159    public Integer getNumTestsPerEvictionRun() {
160        return numTestsPerEvictionRun;
161    }
162
163    public void setNumTestsPerEvictionRun(Integer numTestsPerEvictionRun) {
164        this.numTestsPerEvictionRun = numTestsPerEvictionRun;
165    }
166
167    public Integer getMaxAttempts() {
168        return maxAttempts;
169    }
170
171    public void setMaxAttempts(Integer maxAttempts) {
172        this.maxAttempts = maxAttempts;
173    }
174
175    public boolean isCluster() {
176        return host != null && host.indexOf(",") > 0;
177    }
178
179    public boolean isConfigOk() {
180        return StrUtil.isNotBlank(host);
181    }
182
183    public boolean isClusterConfig() {
184        return isConfigOk() && host.contains(",");
185    }
186
187    public String getType() {
188        return type;
189    }
190
191    public void setType(String type) {
192        this.type = type;
193    }
194
195    public Integer getMaxTotal() {
196        return maxTotal;
197    }
198
199    public void setMaxTotal(Integer maxTotal) {
200        this.maxTotal = maxTotal;
201    }
202
203    public Integer getMaxIdle() {
204        return maxIdle;
205    }
206
207    public void setMaxIdle(Integer maxIdle) {
208        this.maxIdle = maxIdle;
209    }
210
211    public Integer getMinIdle() {
212        return minIdle;
213    }
214
215    public void setMinIdle(Integer minIdle) {
216        this.minIdle = minIdle;
217    }
218
219    public Integer getMaxWaitMillis() {
220        return maxWaitMillis;
221    }
222
223    public void setMaxWaitMillis(Integer maxWaitMillis) {
224        this.maxWaitMillis = maxWaitMillis;
225    }
226
227    public Set<HostAndPort> getHostAndPorts() {
228        Set<HostAndPort> hostAndPortSet = new HashSet<>();
229        String[] hostAndPortStrings = host.split(",");
230        for (String hostAndPortString : hostAndPortStrings) {
231            if (StrUtil.isBlank(hostAndPortString)) continue;
232            String[] hostAndPorts = hostAndPortString.split(":");
233
234            String host = hostAndPorts[0];
235            int port = hostAndPorts.length > 1 ? Integer.parseInt(hostAndPorts[1]) : getPort();
236
237            hostAndPortSet.add(new HostAndPort(host, port));
238        }
239        return hostAndPortSet;
240    }
241
242    public String getSerializer() {
243        return serializer;
244    }
245
246    public void setSerializer(String serializer) {
247        this.serializer = serializer;
248    }
249}