From 33768e107ba9cf8f9a3d20023c1525c14979d6d0 Mon Sep 17 00:00:00 2001 From: CHAE Date: Tue, 31 Oct 2023 21:45:48 +0900 Subject: [PATCH 1/3] =?UTF-8?q?:sparkles:=20Feat:=20Redis=20=EC=9D=98?= =?UTF-8?q?=EC=A1=B4=EC=84=B1=20=EB=B0=8F=20=EC=84=A4=EC=A0=95=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=EC=B6=94=EA=B0=80=20(#44)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 3 ++ .../diareat/config/RedisCacheConfig.java | 28 +++++++++++++++++++ src/main/resources/application.properties | 7 ++++- 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/diareat/diareat/config/RedisCacheConfig.java diff --git a/build.gradle b/build.gradle index ed15302..601d52e 100644 --- a/build.gradle +++ b/build.gradle @@ -49,6 +49,9 @@ dependencies { // Jwt dependency implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.2' // Spring Boot MyBatis implementation "io.jsonwebtoken:jjwt:0.9.1" + + // Redis dependency + implementation 'org.springframework.boot:spring-boot-starter-data-redis' } tasks.named('test') { diff --git a/src/main/java/com/diareat/diareat/config/RedisCacheConfig.java b/src/main/java/com/diareat/diareat/config/RedisCacheConfig.java new file mode 100644 index 0000000..8eeb6bb --- /dev/null +++ b/src/main/java/com/diareat/diareat/config/RedisCacheConfig.java @@ -0,0 +1,28 @@ +package com.diareat.diareat.config; + +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.GenericJackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.RedisSerializationContext; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +import java.time.Duration; + +@Configuration +@EnableCaching +public class RedisCacheConfig { // Redis Cache 설정 + + @Bean + public CacheManager diareatCacheManager(RedisConnectionFactory cf) { + RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() + .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())) + .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())) + .entryTtl(Duration.ofMinutes(3L)); // 캐시 만료 시간 3분 + return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(cf).cacheDefaults(redisCacheConfiguration).build(); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 61a7a3e..98fd09d 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -2,4 +2,9 @@ spring.profiles.include = db # swagger -spring.mvc.pathmatch.matching-strategy=ant_path_matcher \ No newline at end of file +spring.mvc.pathmatch.matching-strategy=ant_path_matcher + +# redis +spring.redis.serializer=org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer +spring.cache.type=redis +spring.cache.redis.cache-null-values=true From 21eff6ba2f362c4a2c419a5f4cfd181588e4fbb4 Mon Sep 17 00:00:00 2001 From: CHAE Date: Wed, 1 Nov 2023 00:06:01 +0900 Subject: [PATCH 2/3] =?UTF-8?q?:sparkles:=20Feat:=20UserService=20?= =?UTF-8?q?=EC=BA=90=EC=8B=B1=20=EC=A0=81=EC=9A=A9=20(#44)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../diareat/user/service/UserService.java | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/diareat/diareat/user/service/UserService.java b/src/main/java/com/diareat/diareat/user/service/UserService.java index 4c877e9..f1206cd 100644 --- a/src/main/java/com/diareat/diareat/user/service/UserService.java +++ b/src/main/java/com/diareat/diareat/user/service/UserService.java @@ -10,6 +10,8 @@ import com.diareat.diareat.util.api.ResponseCode; import com.diareat.diareat.util.exception.UserException; import lombok.RequiredArgsConstructor; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -40,6 +42,7 @@ public Long saveUser(CreateUserDto createUserDto) { } // 회원 기본정보 조회 + @Cacheable(value = "ResponseSimpleUserDto", key = "#userId", cacheManager = "diareatCacheManager") @Transactional(readOnly = true) public ResponseSimpleUserDto getSimpleUserInfo(Long userId) { User user = getUserById(userId); @@ -48,6 +51,7 @@ public ResponseSimpleUserDto getSimpleUserInfo(Long userId) { } // 회원정보 조회 + @Cacheable(value = "ResponseUserDto", key = "#userId", cacheManager = "diareatCacheManager") @Transactional(readOnly = true) public ResponseUserDto getUserInfo(Long userId) { User user = getUserById(userId); @@ -55,6 +59,7 @@ public ResponseUserDto getUserInfo(Long userId) { } // 회원정보 수정 + @CacheEvict(value = {"ResponseSimpleUserDto", "ResponseUserDto"}, key = "#updateUserDto.userId", cacheManager = "diareatCacheManager") @Transactional public void updateUserInfo(UpdateUserDto updateUserDto) { User user = getUserById(updateUserDto.getUserId()); @@ -63,6 +68,7 @@ public void updateUserInfo(UpdateUserDto updateUserDto) { } // 회원 기준섭취량 조회 + @Cacheable(value = "ResponseUserNutritionDto", key = "#userId", cacheManager = "diareatCacheManager") @Transactional(readOnly = true) public ResponseUserNutritionDto getUserNutrition(Long userId) { User user = getUserById(userId); @@ -71,6 +77,7 @@ public ResponseUserNutritionDto getUserNutrition(Long userId) { } // 회원 기준섭취량 직접 수정 + @CacheEvict(value = "ResponseUserNutritionDto", key = "#updateUserNutritionDto.userId", cacheManager = "diareatCacheManager") @Transactional public void updateBaseNutrition(UpdateUserNutritionDto updateUserNutritionDto) { User user = getUserById(updateUserNutritionDto.getUserId()); @@ -80,13 +87,14 @@ public void updateBaseNutrition(UpdateUserNutritionDto updateUserNutritionDto) { } // 회원 탈퇴 + @CacheEvict(value = {"ResponseSimpleUserDto", "ResponseUserDto", "ResponseUserNutritionDto"}, key = "#userId", cacheManager = "diareatCacheManager") @Transactional public void deleteUser(Long userId) { validateUser(userId); userRepository.deleteById(userId); } - // 회원의 친구 검색 결과 조회 + // 회원의 친구 검색 결과 조회 -> 검색 및 팔로우는 굉장히 돌발적으로 이루어질 가능성이 높아 캐시 적용 X @Transactional(readOnly = true) public List searchUser(Long hostId, String name) { validateUser(hostId); @@ -118,15 +126,6 @@ public void unfollowUser(Long userId, Long unfollowId) { followRepository.delete(Follow.makeFollow(userId, unfollowId)); } - // 회원의 팔로우 목록 조회 (현재 외부 Dto 변환은 Food에서 위임받아 진행할지 협의하지 않았기에 일단 User 리스트로 반환) - @Transactional(readOnly = true) - public List getFollowList(Long userId) { - validateUser(userId); - List users = followRepository.findAllByFromUser(userId); - users.add(getUserById(userId)); // 자기 자신도 랭킹에 포함 - return users; - } - private void validateUser(Long userId) { if (!userRepository.existsById(userId)) throw new UserException(ResponseCode.USER_NOT_FOUND); @@ -136,4 +135,4 @@ private User getUserById(Long userId) { return userRepository.findById(userId) .orElseThrow(() -> new UserException(ResponseCode.USER_NOT_FOUND)); } -} \ No newline at end of file +} From 462c8508e4fc71160cb0c88cdc593802086f0d9b Mon Sep 17 00:00:00 2001 From: CHAE Date: Wed, 1 Nov 2023 00:31:23 +0900 Subject: [PATCH 3/3] =?UTF-8?q?:sparkles:=20Feat:=20EnableCaching=20?= =?UTF-8?q?=EC=96=B4=EB=85=B8=ED=85=8C=EC=9D=B4=EC=85=98=20=EB=8F=84?= =?UTF-8?q?=EC=9E=85=20(#44)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/diareat/diareat/DiareatApplication.java | 2 ++ src/main/java/com/diareat/diareat/config/RedisCacheConfig.java | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/diareat/diareat/DiareatApplication.java b/src/main/java/com/diareat/diareat/DiareatApplication.java index 87fdffa..79fcc2e 100644 --- a/src/main/java/com/diareat/diareat/DiareatApplication.java +++ b/src/main/java/com/diareat/diareat/DiareatApplication.java @@ -2,7 +2,9 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; +@EnableCaching @SpringBootApplication public class DiareatApplication { diff --git a/src/main/java/com/diareat/diareat/config/RedisCacheConfig.java b/src/main/java/com/diareat/diareat/config/RedisCacheConfig.java index 8eeb6bb..9d828ec 100644 --- a/src/main/java/com/diareat/diareat/config/RedisCacheConfig.java +++ b/src/main/java/com/diareat/diareat/config/RedisCacheConfig.java @@ -1,7 +1,6 @@ package com.diareat.diareat.config; 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; @@ -14,7 +13,6 @@ import java.time.Duration; @Configuration -@EnableCaching public class RedisCacheConfig { // Redis Cache 설정 @Bean