11package com .ai .lawyer .global .config ;
22
3+ import jakarta .annotation .PreDestroy ;
4+ import lombok .extern .slf4j .Slf4j ;
35import org .springframework .beans .factory .annotation .Value ;
46import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
57import org .springframework .context .annotation .Bean ;
68import 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 ;
712import org .springframework .data .redis .connection .RedisConnectionFactory ;
813import org .springframework .data .redis .connection .lettuce .LettuceConnectionFactory ;
914import org .springframework .data .redis .core .RedisTemplate ;
1015import 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" )
1421public 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