-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRedisTestConfig.java
More file actions
60 lines (52 loc) · 2.52 KB
/
RedisTestConfig.java
File metadata and controls
60 lines (52 loc) · 2.52 KB
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
package com.knu.ddip.config;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@TestConfiguration
public class RedisTestConfig {
@Bean
@Primary
public LettuceConnectionFactory testRedisConnectionFactory() {
String host = System.getProperty("spring.data.redis.host", "localhost");
int port = Integer.parseInt(System.getProperty("spring.data.redis.port", "6379"));
String password = System.getProperty("spring.data.redis.password", "");
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(host, port);
if (!password.isEmpty()) {
config.setPassword(password);
}
return new LettuceConnectionFactory(config);
}
@Bean
@Primary
public RedisTemplate<String, Object> testRedisTemplate(LettuceConnectionFactory testRedisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(testRedisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
template.afterPropertiesSet();
return template;
}
@Bean
@Primary
public RedissonClient testRedissonClient() {
String host = System.getProperty("spring.data.redis.host", "localhost");
int port = Integer.parseInt(System.getProperty("spring.data.redis.port", "6379"));
String password = System.getProperty("spring.data.redis.password", "");
Config config = new Config();
String address = String.format("redis://%s:%d", host, port);
config.useSingleServer()
.setAddress(address)
.setPassword(password.isEmpty() ? null : password);
return Redisson.create(config);
}
}