118 lines
4.9 KiB
Java
118 lines
4.9 KiB
Java
package com.kamco.cd.kamcoback.config;
|
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
|
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
|
|
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
|
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
|
import java.time.Duration;
|
|
import java.util.List;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.cache.CacheManager;
|
|
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.RedisStandaloneConfiguration;
|
|
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;
|
|
|
|
@Configuration
|
|
@EnableCaching
|
|
public class RedisConfig {
|
|
|
|
@Value("${spring.data.redis.host}")
|
|
private String host;
|
|
|
|
@Value("${spring.data.redis.port}")
|
|
private int port;
|
|
|
|
@Value("${spring.data.redis.password}")
|
|
private String password;
|
|
|
|
@Bean
|
|
public RedisConnectionFactory redisConnectionFactory() {
|
|
RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
|
|
redisConfig.setHostName(host);
|
|
redisConfig.setPort(port);
|
|
redisConfig.setPassword(password);
|
|
return new LettuceConnectionFactory(redisConfig);
|
|
}
|
|
|
|
@Bean
|
|
public ObjectMapper redisObjectMapper() {
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
objectMapper.registerModule(new JavaTimeModule());
|
|
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
|
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
|
objectMapper.findAndRegisterModules();
|
|
|
|
return objectMapper;
|
|
}
|
|
|
|
@Bean
|
|
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
|
|
RedisTemplate<String, Object> template = new RedisTemplate<>();
|
|
template.setConnectionFactory(connectionFactory);
|
|
|
|
// Key는 String으로 직렬화
|
|
template.setKeySerializer(new StringRedisSerializer());
|
|
template.setHashKeySerializer(new StringRedisSerializer());
|
|
|
|
// Value는 JSON으로 직렬화 (JavaTimeModule 포함)
|
|
GenericJackson2JsonRedisSerializer serializer =
|
|
new GenericJackson2JsonRedisSerializer(redisObjectMapper());
|
|
template.setValueSerializer(serializer);
|
|
template.setHashValueSerializer(serializer);
|
|
|
|
template.afterPropertiesSet();
|
|
return template;
|
|
}
|
|
|
|
// 기본 레디스 캐시 세팅
|
|
@Bean
|
|
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
|
|
ObjectMapper cacheObjectMapper = new ObjectMapper();
|
|
cacheObjectMapper.registerModule(new JavaTimeModule());
|
|
cacheObjectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
|
cacheObjectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
|
// 알 수 없는 타입에 대해 더 유연하게 처리
|
|
cacheObjectMapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
|
|
cacheObjectMapper.findAndRegisterModules();
|
|
|
|
// 타입 정보 포함 - JAVA_LANG_OBJECT로 제한적으로 적용
|
|
// 호환성 문제 해결을 위해 더 많은 타입 허용
|
|
PolymorphicTypeValidator ptv =
|
|
BasicPolymorphicTypeValidator.builder()
|
|
.allowIfSubType("com.kamco.cd.kamcoback")
|
|
.allowIfSubType("org.springframework.data.domain")
|
|
.allowIfSubType("java.util")
|
|
.allowIfSubType("java.time")
|
|
.allowIfSubType("java.lang")
|
|
.allowIfBaseType(List.class)
|
|
.allowIfBaseType("com.kamco.cd.kamcoback.code.dto.CommonCodeDto$Basic")
|
|
.build();
|
|
cacheObjectMapper.activateDefaultTyping(ptv, ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT);
|
|
|
|
GenericJackson2JsonRedisSerializer serializer =
|
|
new GenericJackson2JsonRedisSerializer(cacheObjectMapper);
|
|
|
|
RedisCacheConfiguration config =
|
|
RedisCacheConfiguration.defaultCacheConfig()
|
|
.entryTtl(Duration.ofHours(1)) // 기본 TTL 1시간
|
|
.serializeKeysWith(
|
|
RedisSerializationContext.SerializationPair.fromSerializer(
|
|
new StringRedisSerializer()))
|
|
.serializeValuesWith(
|
|
RedisSerializationContext.SerializationPair.fromSerializer(serializer));
|
|
|
|
return RedisCacheManager.builder(connectionFactory).cacheDefaults(config).build();
|
|
}
|
|
}
|