package cc.fascinated.bat.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.jedis.JedisClientConfiguration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import java.time.Duration; /** * @author Fascinated (fascinated7) */ @Configuration public class RedisCacheConfig { @Value(value="${spring.redis.host}") private String host; @Value(value="${spring.redis.port}") private String port; @Value(value="${spring.redis.timeout}") private String timeout; @Value(value="${spring.redis.database}") private String database; @Bean JedisConnectionFactory jedisConnectionFactory() { RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(); redisStandaloneConfiguration.setHostName(host); redisStandaloneConfiguration.setPort(Integer.parseInt(port)); redisStandaloneConfiguration.setDatabase(Integer.parseInt(database)); JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder(); jedisClientConfiguration.connectTimeout(Duration.ofSeconds(Integer.parseInt(timeout)));// connection timeout return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration.build()); } @Bean public RedisTemplate redisTemplate() { RedisTemplate template = new RedisTemplate<>(); template.setConnectionFactory(jedisConnectionFactory()); return template; } }