Skip to content

Commit 1270bef

Browse files
committed
Feat: Embedded redis
1 parent 7fa4372 commit 1270bef

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

back/build.gradle.kts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ dependencies {
3333

3434
// Redis
3535
implementation("org.springframework.boot:spring-boot-starter-data-redis")
36-
implementation ("org.springframework.session:spring-session-data-redis")
36+
37+
// Embedded Redis
38+
testImplementation("com.github.codemonstur:embedded-redis:1.4.3")
3739

3840
// Session
3941
implementation("org.springframework.session:spring-session-data-redis")
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.back.global.config;
2+
3+
import jakarta.annotation.PostConstruct;
4+
import jakarta.annotation.PreDestroy;
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.Profile;
8+
import redis.embedded.RedisServer;
9+
10+
import java.io.IOException;
11+
12+
/**
13+
* 로컬 및 테스트 환경 임베디드 Redis 설정
14+
* - test 프로필에서만 활성화
15+
*/
16+
@Configuration
17+
@Profile("test")
18+
public class EmbeddedRedisConfig {
19+
20+
@Value("${spring.data.redis.port}")
21+
private int redisPort;
22+
23+
private RedisServer redisServer;
24+
25+
@PostConstruct
26+
public void startRedis() {
27+
try {
28+
redisServer = new RedisServer(redisPort);
29+
redisServer.start();
30+
System.out.println("========================================");
31+
System.out.println("Embedded Redis started on port " + redisPort);
32+
System.out.println("========================================");
33+
} catch (IOException e) {
34+
System.err.println("Embedded Redis start failed: " + e.getMessage());
35+
System.err.println("This is OK if external Redis is already running");
36+
}
37+
}
38+
39+
@PreDestroy
40+
public void stopRedis() {
41+
try {
42+
if (redisServer != null && redisServer.isActive()) {
43+
redisServer.stop();
44+
System.out.println("========================================");
45+
System.out.println("Embedded Redis stopped");
46+
System.out.println("========================================");
47+
}
48+
} catch (Exception e) {
49+
System.err.println("Error stopping embedded Redis: " + e.getMessage());
50+
}
51+
}
52+
}

back/src/main/resources/application-test.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
spring:
2-
session:
3-
store-type: redis
42
data:
53
redis:
64
host: localhost
@@ -16,4 +14,8 @@ spring:
1614
h2:
1715
console:
1816
enabled: true
19-
path: /h2-console
17+
path: /h2-console
18+
management:
19+
health:
20+
redis:
21+
enabled: false

0 commit comments

Comments
 (0)