1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| @EnableCaching @Configuration public class RedisDevConfiguration { @Bean(name = "redisDevTemplate") public StringRedisTemplate redisTemplate(@Value("${spring.redis.host}") String hostName, @Value("${spring.redis.port}") int port, @Value("${spring.redis.password}") String password, @Value("${spring.redis.lettuce.pool.max-idle}") int maxIdle, @Value("${spring.redis.lettuce.pool.max-active}") int maxTotal, @Value("${spring.redis.database}") int index, @Value("${spring.redis.lettuce.pool.max-wait}") long maxWaitMillis,@Value("${spring.redis.lettuce.pool.min-idle}") int minIdle) { StringRedisTemplate temple = new StringRedisTemplate(); temple.setConnectionFactory( connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis,minIdle));
return temple; }
@Bean(name = "redisUatTemplate") public StringRedisTemplate redisUatTemplate(@Value("${spring.redis2.host}") String hostName, @Value("${spring.redis2.port}") int port, @Value("${spring.redis2.password}") String password, @Value("${spring.redis.lettuce.pool.max-idle}") int maxIdle, @Value("${spring.redis.lettuce.pool.max-active}") int maxTotal, @Value("${spring.redis2.database}") int index, @Value("${spring.redis.lettuce.pool.max-wait}") long maxWaitMillis,@Value("${spring.redis.lettuce.pool.min-idle}") int minIdle) { StringRedisTemplate temple = new StringRedisTemplate(); temple.setConnectionFactory( connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis,minIdle));
return temple; }
public RedisConnectionFactory connectionFactory(String hostName, int port, String password, int maxIdle, int maxTotal, int index, long maxWaitMillis,int minIdle) { JedisConnectionFactory jedis = new JedisConnectionFactory(); jedis.setHostName(hostName); jedis.setPort(port); if (StringUtils.isNotEmpty(password)) { jedis.setPassword(password); } if (index != 0) { jedis.setDatabase(index); } jedis.setPoolConfig(poolCofig(maxIdle, maxTotal, maxWaitMillis,minIdle)); jedis.afterPropertiesSet(); RedisConnectionFactory factory = jedis;
return factory; }
public JedisPoolConfig poolCofig(int maxIdle, int maxTotal, long maxWaitMillis,int minIdle) { JedisPoolConfig poolCofig = new JedisPoolConfig(); poolCofig.setMaxIdle(maxIdle); poolCofig.setMaxTotal(maxTotal); poolCofig.setMaxWaitMillis(maxWaitMillis); poolCofig.setMinIdle(minIdle); return poolCofig; } }
|