Skip to content

Commit e56459e

Browse files
committed
fix[member]: 로컬 레디스로 연결될 때 로그 추가
1 parent e4a744b commit e56459e

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

backend/src/main/java/com/ai/lawyer/global/config/RedisConfig.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package com.ai.lawyer.global.config;
22

3+
import jakarta.annotation.PreDestroy;
4+
import lombok.extern.slf4j.Slf4j;
35
import org.springframework.beans.factory.annotation.Value;
46
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
57
import org.springframework.context.annotation.Bean;
68
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.context.event.ContextClosedEvent;
10+
import org.springframework.context.event.ContextRefreshedEvent;
11+
import org.springframework.context.event.EventListener;
712
import org.springframework.data.redis.connection.RedisConnectionFactory;
813
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
914
import org.springframework.data.redis.core.RedisTemplate;
1015
import org.springframework.data.redis.serializer.StringRedisSerializer;
16+
import redis.embedded.RedisServer;
1117

18+
@Slf4j
1219
@Configuration
1320
@ConditionalOnProperty(name = "spring.data.redis.embedded", havingValue = "false")
1421
public class RedisConfig {
@@ -19,8 +26,11 @@ public class RedisConfig {
1926
@Value("${spring.data.redis.port:6379}")
2027
private int redisPort;
2128

29+
private RedisServer redisServer;
30+
2231
@Bean
2332
public RedisConnectionFactory redisConnectionFactory() {
33+
log.info("=== RedisConnectionFactory 생성: host={}, port={} ===", redisHost, redisPort);
2434
return new LettuceConnectionFactory(redisHost, redisPort);
2535
}
2636

@@ -34,6 +44,33 @@ public RedisTemplate<String, Object> redisTemplate() {
3444
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
3545
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
3646

47+
log.info("=== RedisTemplate 설정 완료 (host={}, port={}) ===", redisHost, redisPort);
3748
return redisTemplate;
3849
}
50+
51+
@EventListener(ContextRefreshedEvent.class)
52+
public void startRedis() {
53+
try {
54+
redisServer = RedisServer.builder()
55+
.port(redisPort)
56+
.setting("maxmemory 128M")
57+
.build();
58+
59+
if (!redisServer.isActive()) {
60+
redisServer.start();
61+
log.info("=== Redis 서버가 포트 {}에서 시작되었습니다 ===", redisPort);
62+
}
63+
} catch (Exception e) {
64+
log.error("=== Redis 서버 시작 실패: {} ===", e.getMessage(), e);
65+
}
66+
}
67+
68+
@PreDestroy
69+
@EventListener(ContextClosedEvent.class)
70+
public void stopRedis() {
71+
if (redisServer != null && redisServer.isActive()) {
72+
redisServer.stop();
73+
log.info("=== Redis 서버가 중지되었습니다 ===");
74+
}
75+
}
3976
}

0 commit comments

Comments
 (0)