From e27228023026b059a995f3130b5e3cb6cb13141f Mon Sep 17 00:00:00 2001 From: songhyeonpk Date: Wed, 12 Mar 2025 17:51:08 +0900 Subject: [PATCH 1/2] =?UTF-8?q?chore:=20Redis=20Session=20timeout=20?= =?UTF-8?q?=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index fb80e35..b1bbd1f 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -9,6 +9,9 @@ spring: - storage - security + session: + timeout: 1800 # 30분 + mail: host: smtp.gmail.com port: 587 From ae52f8838442a3b9fd0b4762a1b3dc47b5b446f6 Mon Sep 17 00:00:00 2001 From: songhyeonpk Date: Fri, 14 Mar 2025 15:39:03 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20Redis=20=EC=84=A4=EC=A0=95=20?= =?UTF-8?q?=EB=B0=8F=20=EC=BA=90=EC=8B=B1/=EC=84=B8=EC=85=98=20=EC=9D=B8?= =?UTF-8?q?=ED=84=B0=ED=8E=98=EC=9D=B4=EC=8A=A4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ftm/server/adapter/gateway/.gitkeep | 0 .../adapter/gateway/RedisCacheGateway.java | 4 ++ .../adapter/gateway/RedisSessionGateway.java | 4 ++ .../adapter/gateway/repository/.gitkeep | 0 .../ftm/server/infrastructure/redis/.gitkeep | 0 .../infrastructure/redis/RedisConfig.java | 65 +++++++++++++++++++ .../infrastructure/redis/RedisProperties.java | 16 +++++ .../infrastructure/redis/RedisService.java | 15 +++++ 8 files changed, 104 insertions(+) delete mode 100644 src/main/java/com/ftm/server/adapter/gateway/.gitkeep create mode 100644 src/main/java/com/ftm/server/adapter/gateway/RedisCacheGateway.java create mode 100644 src/main/java/com/ftm/server/adapter/gateway/RedisSessionGateway.java delete mode 100644 src/main/java/com/ftm/server/adapter/gateway/repository/.gitkeep delete mode 100644 src/main/java/com/ftm/server/infrastructure/redis/.gitkeep create mode 100644 src/main/java/com/ftm/server/infrastructure/redis/RedisConfig.java create mode 100644 src/main/java/com/ftm/server/infrastructure/redis/RedisProperties.java create mode 100644 src/main/java/com/ftm/server/infrastructure/redis/RedisService.java diff --git a/src/main/java/com/ftm/server/adapter/gateway/.gitkeep b/src/main/java/com/ftm/server/adapter/gateway/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/com/ftm/server/adapter/gateway/RedisCacheGateway.java b/src/main/java/com/ftm/server/adapter/gateway/RedisCacheGateway.java new file mode 100644 index 0000000..19a2b6f --- /dev/null +++ b/src/main/java/com/ftm/server/adapter/gateway/RedisCacheGateway.java @@ -0,0 +1,4 @@ +package com.ftm.server.adapter.gateway; + +/** Redis Caching 관련 작업 Gateway */ +public interface RedisCacheGateway {} diff --git a/src/main/java/com/ftm/server/adapter/gateway/RedisSessionGateway.java b/src/main/java/com/ftm/server/adapter/gateway/RedisSessionGateway.java new file mode 100644 index 0000000..31c06b2 --- /dev/null +++ b/src/main/java/com/ftm/server/adapter/gateway/RedisSessionGateway.java @@ -0,0 +1,4 @@ +package com.ftm.server.adapter.gateway; + +/** Redis Session 작업 관련 Gateway */ +public interface RedisSessionGateway {} diff --git a/src/main/java/com/ftm/server/adapter/gateway/repository/.gitkeep b/src/main/java/com/ftm/server/adapter/gateway/repository/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/com/ftm/server/infrastructure/redis/.gitkeep b/src/main/java/com/ftm/server/infrastructure/redis/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/com/ftm/server/infrastructure/redis/RedisConfig.java b/src/main/java/com/ftm/server/infrastructure/redis/RedisConfig.java new file mode 100644 index 0000000..607cc49 --- /dev/null +++ b/src/main/java/com/ftm/server/infrastructure/redis/RedisConfig.java @@ -0,0 +1,65 @@ +package com.ftm.server.infrastructure.redis; + +import java.time.Duration; +import lombok.RequiredArgsConstructor; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.cache.RedisCacheConfiguration; +import org.springframework.data.redis.cache.RedisCacheManager; +import org.springframework.data.redis.connection.RedisConnectionFactory; +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.RedisSerializationContext; +import org.springframework.data.redis.serializer.StringRedisSerializer; +import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; + +@EnableRedisHttpSession +@EnableCaching +@Configuration +@RequiredArgsConstructor +public class RedisConfig { + + private final RedisProperties redisProperties; + + // Redis Connection + @Bean + public RedisConnectionFactory redisConnectionFactory() { + return new LettuceConnectionFactory(redisProperties.getHost(), redisProperties.getPort()); + } + + // Redis Template + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory factory) { + RedisTemplate template = new RedisTemplate<>(); + template.setConnectionFactory(factory); + + // 단순 Key-Value 직렬화 + template.setKeySerializer(new StringRedisSerializer()); + template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); + + // 해시 Key-Value 직렬화 + template.setHashKeySerializer(new StringRedisSerializer()); + template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); + + template.afterPropertiesSet(); + return template; + } + + // Redis Caching + @Bean + public RedisCacheManager redisCacheManager(RedisConnectionFactory factory) { + RedisCacheConfiguration cacheConfiguration = + RedisCacheConfiguration.defaultCacheConfig() + .entryTtl(Duration.ofMinutes(30)) // 기본 캐시 만료시간 30분 + .serializeKeysWith( + RedisSerializationContext.SerializationPair.fromSerializer( + new StringRedisSerializer())) + .serializeValuesWith( + RedisSerializationContext.SerializationPair.fromSerializer( + new GenericJackson2JsonRedisSerializer())); + + return RedisCacheManager.builder(factory).cacheDefaults(cacheConfiguration).build(); + } +} diff --git a/src/main/java/com/ftm/server/infrastructure/redis/RedisProperties.java b/src/main/java/com/ftm/server/infrastructure/redis/RedisProperties.java new file mode 100644 index 0000000..b097ed6 --- /dev/null +++ b/src/main/java/com/ftm/server/infrastructure/redis/RedisProperties.java @@ -0,0 +1,16 @@ +package com.ftm.server.infrastructure.redis; + +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConfigurationProperties(prefix = "spring.data.redis") +@Getter +@Setter +public class RedisProperties { + + private String host; + private int port; +} diff --git a/src/main/java/com/ftm/server/infrastructure/redis/RedisService.java b/src/main/java/com/ftm/server/infrastructure/redis/RedisService.java new file mode 100644 index 0000000..7d83c96 --- /dev/null +++ b/src/main/java/com/ftm/server/infrastructure/redis/RedisService.java @@ -0,0 +1,15 @@ +package com.ftm.server.infrastructure.redis; + +import com.ftm.server.adapter.gateway.RedisCacheGateway; +import com.ftm.server.adapter.gateway.RedisSessionGateway; +import lombok.RequiredArgsConstructor; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; + +/** Redis Caching, Session 구현체 각 역할 별 레디스 조작 관리 (비즈니스 로직이 포함되면 안됨, 기술적인 로직만 수행) */ +@Service +@RequiredArgsConstructor +public class RedisService implements RedisCacheGateway, RedisSessionGateway { + + private final RedisTemplate redisTemplate; +}