cl
5 天以前 926cc0fd4fad17ae02814387ced7e0a88b042a2b
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
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<Object, Object> redisTemplate(
            @Qualifier("openApiTokenRedisConnectionFactory") LettuceConnectionFactory openApiTokenRedisConnectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(openApiTokenRedisConnectionFactory);
        return template;
    }
}