@Configuration
public class ChargeRedisConfig {@Value("${spring.redis.host}")private String host;@Value("${spring.redis.port}")private int port;@Value("${spring.redis.password}")private String password;@Value("${spring.redis.chargeDatabase:1}")private int chargeDatabase;@Bean("chargeRedisTemplate")public RedisTemplate<String, Object> chargeRedisTemplate() {RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();redisStandaloneConfiguration.setHostName(host);redisStandaloneConfiguration.setPort(port);redisStandaloneConfiguration.setPassword(password);redisStandaloneConfiguration.setDatabase(chargeDatabase);LettuceConnectionFactory factory = new LettuceConnectionFactory(redisStandaloneConfiguration);factory.afterPropertiesSet();RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(factory);template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new GenericFastJsonRedisSerializer());template.setHashKeySerializer(new StringRedisSerializer());template.setHashValueSerializer(new GenericFastJsonRedisSerializer());return template;}
}
spring:redis:enabled: truetype: 1host: 192.168.1.1port: 6379password: 123456timeout: 10000# 默认databasedatabase: 1# 收费databasechargeDatabase: 2ttl: 60lettuce:pool:max-active: 500max-idle: 10min-idle: 2max-wait: 3000shutdown-timeout: 10000command-timeout: 60000jackson:date-format: yyyy-MM-dd HH:mm:sstime-zone: GMT+8
使用
@Resource
private RedisTemplate<String, Object> chargeRedisTemplate;String key = "123";
Object o = chargeRedisTemplate.opsForValue().get(key);