Skip to content

Commit

Permalink
Merge pull request #62 from SWM-KAWAI-MANS/feat/cache-single-data
Browse files Browse the repository at this point in the history
single 기록을 캐싱한다.
  • Loading branch information
seong-wooo authored Nov 8, 2023
2 parents aeb2771 + 79c4a8b commit f299fbe
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
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;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
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 {

Expand All @@ -39,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 @@ -59,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 f299fbe

Please sign in to comment.