diff --git a/src/main/java/online/partyrun/partyrunbattleservice/domain/single/service/SingleService.java b/src/main/java/online/partyrun/partyrunbattleservice/domain/single/service/SingleService.java index 7ad94c8b..157a953c 100644 --- a/src/main/java/online/partyrun/partyrunbattleservice/domain/single/service/SingleService.java +++ b/src/main/java/online/partyrun/partyrunbattleservice/domain/single/service/SingleService.java @@ -12,6 +12,7 @@ import online.partyrun.partyrunbattleservice.domain.single.exception.InvalidSingleOwnerException; import online.partyrun.partyrunbattleservice.domain.single.exception.SingleNotFoundException; import online.partyrun.partyrunbattleservice.domain.single.repository.SingleRepository; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import java.util.List; @@ -29,6 +30,7 @@ public SingleIdResponse create(String runnerId, SingleRunnerRecordsRequest reque return new SingleIdResponse(newSingleRecord.getId()); } + @Cacheable(value = "single", key = "#singleId.concat('-').concat(#runnerId)") public SingleResponse getSingle(String singleId, String runnerId) { final Single single = singleRepository.findById(singleId).orElseThrow(() -> new SingleNotFoundException(singleId)); if (!single.isOwner(runnerId)) { diff --git a/src/main/java/online/partyrun/partyrunbattleservice/domain/battle/config/RedisConfig.java b/src/main/java/online/partyrun/partyrunbattleservice/global/config/RedisConfig.java similarity index 58% rename from src/main/java/online/partyrun/partyrunbattleservice/domain/battle/config/RedisConfig.java rename to src/main/java/online/partyrun/partyrunbattleservice/global/config/RedisConfig.java index f5684f30..c6ee9928 100644 --- a/src/main/java/online/partyrun/partyrunbattleservice/domain/battle/config/RedisConfig.java +++ b/src/main/java/online/partyrun/partyrunbattleservice/global/config/RedisConfig.java @@ -1,4 +1,4 @@ -package online.partyrun.partyrunbattleservice.domain.battle.config; +package online.partyrun.partyrunbattleservice.global.config; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; @@ -6,21 +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.mongodb.core.aggregation.SelectionOperators; +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 { @@ -39,13 +44,17 @@ public Topic topic() { @Bean public RedisSerializer 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 @@ -59,4 +68,24 @@ public RedisTemplate 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 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(); + } }