package com.ssi.config; import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import java.time.Duration; import java.util.List; /** * JetCache使用redis做缓存配置 * 和 spring boot redis 使用同一份配置 * * @author LiXiaoCong * @version 2018/11/18 9:52 */ @Slf4j @Getter @Setter @Configuration @ConfigurationProperties(prefix = "spring.redis") public class JetCacheRedisConfig { /** * Database index used by the connection factory. */ private int database = 0; /** * Connection URL. Overrides host, port, and password. User is ignored. Example: * redis://user:password@example.com:6379 */ private String url; /** * Redis server host. */ private String host = "localhost"; /** * Login password of the redis server. */ private String password; /** * Redis server port. */ private int port = 6379; /** * Whether to enable SSL support. */ private boolean ssl; /** * Connection timeout. */ private Duration timeout; private Sentinel sentinel; private SingleConfig singleNode; private Pool pool; private final Jedis jedis = new Jedis(); private final Lettuce lettuce = new Lettuce(); public Jedis getJedis() { this.jedis.setPool(pool); return this.jedis; } /** * Pool properties. */ public static class Pool { /** * Maximum number of "idle" connections in the pool. Use a negative value to * indicate an unlimited number of idle connections. */ private int maxIdle = 8; /** * Target for the minimum number of idle connections to maintain in the pool. This * setting only has an effect if it is positive. */ private int minIdle = 0; /** * Maximum number of connections that can be allocated by the pool at a given * time. Use a negative value for no limit. */ private int maxActive = 8; /** * Maximum amount of time a connection allocation should block before throwing an * exception when the pool is exhausted. Use a negative value to block * indefinitely. */ private Duration maxWait = Duration.ofMillis(-1); public int getMaxIdle() { return this.maxIdle; } public void setMaxIdle(int maxIdle) { this.maxIdle = maxIdle; } public int getMinIdle() { return this.minIdle; } public void setMinIdle(int minIdle) { this.minIdle = minIdle; } public int getMaxActive() { return this.maxActive; } public void setMaxActive(int maxActive) { this.maxActive = maxActive; } public Duration getMaxWait() { return this.maxWait; } public void setMaxWait(Duration maxWait) { this.maxWait = maxWait; } } /** * Redis sentinel properties. */ public static class Sentinel { /** * Name of the Redis server. */ private String master; /** * Comma-separated list of "host:port" pairs. */ private List nodes; public String getMaster() { return this.master; } public void setMaster(String master) { this.master = master; } public List getNodes() { return this.nodes; } public void setNodes(List nodes) { this.nodes = nodes; } } /** * Jedis client properties. */ public static class Jedis { /** * Jedis pool configuration. */ private Pool pool; public Pool getPool() { return this.pool; } public void setPool(Pool pool) { this.pool = pool; } } /** * Lettuce client properties. */ public static class Lettuce { /** * Shutdown timeout. */ private Duration shutdownTimeout = Duration.ofMillis(100); /** * Lettuce pool configuration. */ private Pool pool; public Duration getShutdownTimeout() { return this.shutdownTimeout; } public void setShutdownTimeout(Duration shutdownTimeout) { this.shutdownTimeout = shutdownTimeout; } public Pool getPool() { return this.pool; } public void setPool(Pool pool) { this.pool = pool; } } public static class SingleConfig { private String host; private Integer port; private Integer database; private String password; public String getHost() { return host; } public void setHost(String host) { this.host = host; } public Integer getPort() { return port; } public void setPort(Integer port) { this.port = port; } public Integer getDatabase() { return database; } public void setDatabase(Integer database) { this.database = database; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } }