Skip to content

Commit 0f013c5

Browse files
authored
Chore: Redis 설정 추가 (#57) (#59)
* Chore: Redis 설정 추가 * Test: Redis 연결 테스트 * Ref: 내장 Redis 설정 개선 * Ref: 내장 Redis 의존성 변경
1 parent 33c5d40 commit 0f013c5

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ dependencies {
6363
testImplementation("org.springframework.boot:spring-boot-starter-test")
6464
testImplementation("org.springframework.security:spring-security-test")
6565
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
66+
67+
// Redis
68+
implementation("org.springframework.boot:spring-boot-starter-data-redis")
69+
implementation("com.github.codemonstur:embedded-redis:1.4.3")
6670
}
6771

6872
tasks.withType<Test> {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.back.global.config;
2+
3+
import jakarta.annotation.PostConstruct;
4+
import jakarta.annotation.PreDestroy;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.annotation.Profile;
7+
import redis.embedded.RedisServer;
8+
9+
import java.io.IOException;
10+
import java.net.ServerSocket;
11+
12+
/**
13+
* Embedded Redis 설정 클래스
14+
* - 개발 및 테스트 환경에서만 활성화 (dev, test 프로파일)
15+
* - 애플리케이션 시작 시 내장 Redis 서버 시작
16+
* - 애플리케이션 종료 시 내장 Redis 서버 중지
17+
*/
18+
@Configuration
19+
@Profile({"dev", "test"})
20+
public class EmbeddedRedisConfig {
21+
22+
private RedisServer redisServer;
23+
private int port;
24+
25+
@PostConstruct
26+
public void startRedis() {
27+
try {
28+
this.port = findAvailablePort();
29+
this.redisServer = new RedisServer(port);
30+
this.redisServer.start();
31+
System.setProperty("spring.data.redis.port", String.valueOf(port));
32+
} catch (IOException e) {
33+
throw new IllegalStateException("Failed to start embedded Redis", e);
34+
}
35+
}
36+
37+
@PreDestroy
38+
public void stopRedis() {
39+
try {
40+
if (redisServer != null && redisServer.isActive()) {
41+
redisServer.stop();
42+
}
43+
} catch (IOException e) {
44+
throw new IllegalStateException("Failed to stop embedded Redis", e);
45+
}
46+
}
47+
48+
private int findAvailablePort() {
49+
try (ServerSocket socket = new ServerSocket(0)) {
50+
socket.setReuseAddress(true);
51+
return socket.getLocalPort();
52+
} catch (IOException e) {
53+
throw new IllegalStateException("No available port found", e);
54+
}
55+
}
56+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.back.global.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.data.redis.connection.RedisConnectionFactory;
6+
import org.springframework.data.redis.core.RedisTemplate;
7+
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
8+
import org.springframework.data.redis.serializer.StringRedisSerializer;
9+
10+
/**
11+
* Redis 설정 클래스
12+
*/
13+
@Configuration
14+
public class RedisConfig {
15+
16+
/**
17+
* RedisTemplate 설정
18+
* - Key: String
19+
* - Value: JSON (Jackson)
20+
*/
21+
@Bean
22+
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
23+
RedisTemplate<String, Object> template = new RedisTemplate<>();
24+
template.setConnectionFactory(connectionFactory);
25+
26+
// Key: String
27+
template.setKeySerializer(new StringRedisSerializer());
28+
template.setHashKeySerializer(new StringRedisSerializer());
29+
30+
// Value: JSON
31+
template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
32+
template.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
33+
34+
return template;
35+
}
36+
}

src/main/resources/application-dev.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ spring:
55
username: sa
66
password:
77

8+
data:
9+
redis:
10+
host: localhost
11+
port: 6379
12+
813
config:
914
import: optional:file:.env[.properties]
1015

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.back;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.data.redis.core.StringRedisTemplate;
7+
import org.springframework.test.context.ActiveProfiles;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
@SpringBootTest
12+
@ActiveProfiles("test")
13+
class RedisConnectionTest {
14+
15+
@Autowired
16+
private StringRedisTemplate redisTemplate;
17+
18+
@Test
19+
void redisShouldStoreAndGetValue() {
20+
// given
21+
String key = "test:key";
22+
String value = "hello";
23+
24+
// when
25+
redisTemplate.opsForValue().set(key, value);
26+
String result = redisTemplate.opsForValue().get(key);
27+
28+
// then
29+
assertThat(result).isEqualTo(value);
30+
}
31+
}

0 commit comments

Comments
 (0)