package com.vincent.rsf.openApi.config; import org.springframework.beans.factory.annotation.Qualifier; 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.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; /** * 业务 Redis({@code redis.index})与默认 {@code redisTemplate};与电视机侧 {@code tv-monitor.redis.database} 分离 */ @Configuration public class OpenApiTokenRedisConfig { @Value("${redis.host:127.0.0.1}") private String host; @Value("${redis.port:6379}") private int port; @Value("${redis.password:}") private String password; @Value("${redis.index:0}") private int database; @Bean(name = "openApiTokenRedisConnectionFactory") public LettuceConnectionFactory openApiTokenRedisConnectionFactory() { RedisStandaloneConfiguration cfg = new RedisStandaloneConfiguration(host, port); cfg.setDatabase(database); if (password != null && !password.isEmpty()) { cfg.setPassword(password); } return new LettuceConnectionFactory(cfg); } /** * 存在多个 ConnectionFactory 时 Boot 不会装配默认 redisTemplate */ @Bean(name = "redisTemplate") public RedisTemplate redisTemplate( @Qualifier("openApiTokenRedisConnectionFactory") LettuceConnectionFactory openApiTokenRedisConnectionFactory) { RedisTemplate template = new RedisTemplate<>(); template.setConnectionFactory(openApiTokenRedisConnectionFactory); return template; } }