-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedisConfig.java
96 lines (78 loc) · 3.99 KB
/
RedisConfig.java
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.devcourse.web2_1_dashbunny_be.config;
import com.devcourse.web2_1_dashbunny_be.domain.owner.MenuManagement;
import com.order.generated.OrdersProtobuf;
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.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Value("${spring.data.redis.host}")
private String redisHost;
@Value("${spring.data.redis.port}")
private String redisPort;
@Bean
public RedisConnectionFactory redisConnectionFactory () {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(redisHost);
redisStandaloneConfiguration.setPort(Integer.parseInt(redisPort));
return new LettuceConnectionFactory(redisStandaloneConfiguration);
}
@Bean(name = "genericRedisTemplate")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
// Key를 String으로 직렬화
template.setKeySerializer(new StringRedisSerializer());
// Value를 JSON으로 직렬화
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return template;
}
// MenuManagement 용도의 RedisTemplate
@Bean(name = "menuRedisTemplate")
public RedisTemplate<String, MenuManagement> menuRedisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, MenuManagement> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
// Key를 String으로 직렬화
template.setKeySerializer(new StringRedisSerializer());
// Value를 JSON으로 직렬화
Jackson2JsonRedisSerializer<MenuManagement> serializer = new Jackson2JsonRedisSerializer<>(MenuManagement.class);
template.setValueSerializer(serializer);
template.setHashValueSerializer(serializer);
// After properties
template.afterPropertiesSet();
return template;
}
// Order 용도의 RedisTemplate
@Bean(name = "orderRedisTemplate")
public RedisTemplate<String, OrdersProtobuf.Orders> orderRedisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, OrdersProtobuf.Orders> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
// Protobuf 직렬화기 생성
ProtobufRedisSerializer<OrdersProtobuf.Orders> protobufSerializer = new ProtobufRedisSerializer<>(OrdersProtobuf.Orders.parser());
// 직렬화 설정
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setValueSerializer(protobufSerializer);
redisTemplate.setHashKeySerializer(RedisSerializer.string());
redisTemplate.setHashValueSerializer(protobufSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
@Bean
public HashOperations<String, String, OrdersProtobuf.Orders> hash(@Qualifier("orderRedisTemplate") RedisTemplate<String, OrdersProtobuf.Orders> redisTemplate) {
return redisTemplate.opsForHash();
}
@Bean
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForHash();
}
}