Skip to content

Commit

Permalink
feat: CacheManager Bean 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
seong-wooo committed Sep 20, 2023
1 parent 776059b commit 9985160
Showing 1 changed file with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import online.partyrun.partyrunbattleservice.domain.battle.dto.BattleWebSocketResponse;
import online.partyrun.partyrunbattleservice.domain.battle.infra.RedisSubscriber;
import online.partyrun.partyrunbattleservice.domain.single.dto.SingleResponse;
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.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.serializer.*;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;

@EnableCaching
@Configuration
public class RedisConfig {

Expand All @@ -38,13 +44,17 @@ public Topic topic() {

@Bean
public RedisSerializer<BattleWebSocketResponse> redisSerializer() {
return new Jackson2JsonRedisSerializer<>(objectMapper(), BattleWebSocketResponse.class);
}

private ObjectMapper objectMapper() {
final JavaTimeModule module = new JavaTimeModule();
module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ISO_LOCAL_DATE_TIME));

final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(module);
return new Jackson2JsonRedisSerializer<>(objectMapper, BattleWebSocketResponse.class);
return objectMapper;
}

@Bean
Expand All @@ -58,4 +68,24 @@ public RedisTemplate<String, BattleWebSocketResponse> redisTemplate(RedisConnect

return redisTemplate;
}

@Bean
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
final RedisCacheConfiguration defaultConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofHours(1))
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer(objectMapper())));

final HashMap<String, RedisCacheConfiguration> configurations = new HashMap<>();
configurations.put("single", defaultConfiguration.entryTtl(Duration.ofMinutes(30))
.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(new Jackson2JsonRedisSerializer<>(objectMapper(), SingleResponse.class)))
);

return RedisCacheManager.RedisCacheManagerBuilder
.fromConnectionFactory(connectionFactory)
.cacheDefaults(defaultConfiguration)
.withInitialCacheConfigurations(configurations)
.build();
}
}

0 comments on commit 9985160

Please sign in to comment.