Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.ftm.server.adapter.gateway;

/** Redis Caching 관련 작업 Gateway */
public interface RedisCacheGateway {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.ftm.server.adapter.gateway;

/** Redis Session 작업 관련 Gateway */
public interface RedisSessionGateway {}
Empty file.
Empty file.
65 changes: 65 additions & 0 deletions src/main/java/com/ftm/server/infrastructure/redis/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> redisTemplate;
}
3 changes: 3 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ spring:
- storage
- security

session:
timeout: 1800 # 30분

mail:
host: smtp.gmail.com
port: 587
Expand Down