From 776059bd2b25582e7c0a7e5c4f221f9418be59ca Mon Sep 17 00:00:00 2001 From: seongwoo Date: Wed, 20 Sep 2023 13:37:27 +0900 Subject: [PATCH 1/3] =?UTF-8?q?refactor:=20RedisConfig=20=ED=8C=A8?= =?UTF-8?q?=ED=82=A4=EC=A7=80=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{domain/battle => global}/config/RedisConfig.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) rename src/main/java/online/partyrun/partyrunbattleservice/{domain/battle => global}/config/RedisConfig.java (95%) 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 95% 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..4ad837e2 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; @@ -8,7 +8,6 @@ import online.partyrun.partyrunbattleservice.domain.battle.infra.RedisSubscriber; 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.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.listener.ChannelTopic; From 9985160bb6c73f746655e863db3949ef04c4d66c Mon Sep 17 00:00:00 2001 From: seongwoo Date: Wed, 20 Sep 2023 15:38:57 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20CacheManager=20Bean=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/config/RedisConfig.java | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/main/java/online/partyrun/partyrunbattleservice/global/config/RedisConfig.java b/src/main/java/online/partyrun/partyrunbattleservice/global/config/RedisConfig.java index 4ad837e2..c6ee9928 100644 --- a/src/main/java/online/partyrun/partyrunbattleservice/global/config/RedisConfig.java +++ b/src/main/java/online/partyrun/partyrunbattleservice/global/config/RedisConfig.java @@ -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 { @@ -38,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 @@ -58,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(); + } } From 79c4a8b85b29a81a2aaff36c251bf0c6ab1464d8 Mon Sep 17 00:00:00 2001 From: seongwoo Date: Wed, 20 Sep 2023 15:50:08 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20single=20=EA=B2=B0=EA=B3=BC=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=EC=97=90=20=EC=BA=90=EC=8B=B1=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/single/service/SingleService.java | 2 ++ 1 file changed, 2 insertions(+) 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)) {