-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #210 from gamemuncheol/perf/209
Cacheable Annotation 사용해서 Post Detail Caching 하는 방식으로 변경
- Loading branch information
Showing
16 changed files
with
134 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
src/main/java/com/gamemoonchul/application/PostCacheService.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/com/gamemoonchul/common/constants/RedisKeyConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.gamemoonchul.common.constants; | ||
|
||
public class RedisKeyConstant { | ||
public static String postCountKey(Long postId) { | ||
return "post:count:" + postId; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/gamemoonchul/config/RedisCacheManagerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.gamemoonchul.config; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.gamemoonchul.domain.entity.Post; | ||
import lombok.RequiredArgsConstructor; | ||
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.serializer.Jackson2JsonRedisSerializer; | ||
import org.springframework.data.redis.serializer.RedisSerializationContext; | ||
import org.springframework.data.redis.serializer.RedisSerializer; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
import java.time.Duration; | ||
|
||
@Configuration | ||
@EnableCaching | ||
@RequiredArgsConstructor | ||
public class RedisCacheManagerConfig { | ||
private final ObjectMapper objectMapper; | ||
|
||
@Bean | ||
public CacheManager postCacheManager(RedisConnectionFactory cf) { | ||
RedisSerializer<Post> valueSerializer = new Jackson2JsonRedisSerializer<>(objectMapper.getTypeFactory().constructType(Post.class)); | ||
|
||
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() | ||
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())) | ||
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer)) // Value Serializer 변경 | ||
.entryTtl(Duration.ofMinutes(30L)); // 캐시 수명 30분 | ||
|
||
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(cf).cacheDefaults(redisCacheConfiguration).build(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,67 @@ | ||
package com.gamemoonchul.config; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.gamemoonchul.domain.entity.Member; | ||
import com.gamemoonchul.domain.entity.Post; | ||
import com.gamemoonchul.infrastructure.web.dto.response.PostMainPageResponse; | ||
import lombok.RequiredArgsConstructor; | ||
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.serializer.GenericJackson2JsonRedisSerializer; | ||
import org.springframework.data.redis.serializer.GenericToStringSerializer; | ||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; | ||
import org.springframework.data.redis.serializer.RedisSerializationContext; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
import java.util.List; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class RedisConfig { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
@Bean | ||
RedisTemplate<String, Member> userRedisTemplate(RedisConnectionFactory connectionFactory) { | ||
var template = new RedisTemplate<String, Member>(); | ||
public RedisTemplate<String, Integer> stringIntegerRedisTemplate(RedisConnectionFactory connectionFactory) { | ||
RedisTemplate<String, Integer> template = new RedisTemplate<>(); | ||
template.setConnectionFactory(connectionFactory); | ||
|
||
// Key Serializer 설정 | ||
template.setKeySerializer(new StringRedisSerializer()); | ||
template.setValueSerializer(new Jackson2JsonRedisSerializer<>(objectMapper, Member.class)); | ||
|
||
// Value Serializer 설정 | ||
template.setValueSerializer(new GenericToStringSerializer<>(Integer.class)); | ||
|
||
return template; | ||
} | ||
|
||
/** | ||
* Redis Cacheable에서 사용함 | ||
* | ||
* @param connectionFactory | ||
* @return redisTemplate | ||
*/ | ||
@Bean | ||
public RedisTemplate<String, List<PostMainPageResponse>> postDetailRedisTemplate(RedisConnectionFactory connectionFactory, ObjectMapper objectMapper) { | ||
RedisTemplate<String, List<PostMainPageResponse>> template = new RedisTemplate<>(); | ||
public RedisTemplate<String, Post> postRedisTemplate(RedisConnectionFactory connectionFactory) { | ||
RedisTemplate<String, Post> template = new RedisTemplate<>(); | ||
template.setConnectionFactory(connectionFactory); | ||
|
||
// Key Serializer 설정 | ||
template.setKeySerializer(new GenericToStringSerializer<>(String.class)); | ||
template.setKeySerializer(new StringRedisSerializer()); | ||
|
||
// Value Serializer 설정 | ||
Jackson2JsonRedisSerializer<List<PostMainPageResponse>> serializer = | ||
new Jackson2JsonRedisSerializer<>(objectMapper.getTypeFactory().constructCollectionType(List.class, PostMainPageResponse.class)); | ||
Jackson2JsonRedisSerializer<Post> serializer = | ||
new Jackson2JsonRedisSerializer<>(objectMapper.getTypeFactory().constructType(Post.class)); | ||
template.setValueSerializer(serializer); | ||
|
||
template.setValueSerializer(serializer); | ||
template.setValueSerializer(serializer); | ||
|
||
template.afterPropertiesSet(); | ||
return template; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.