From 90ae0a08ee76ee67787b3eb15e05757d628ea176 Mon Sep 17 00:00:00 2001 From: its-sky Date: Wed, 10 Apr 2024 16:11:15 +0900 Subject: [PATCH 1/4] =?UTF-8?q?[Refactor]=20#263=20-=20=EC=9C=A0=EC=A0=80?= =?UTF-8?q?=20=EC=A1=B0=ED=9A=8C=20Redis=20=EC=BA=90=EC=8B=9C=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../objective/service/ObjectiveService.java | 2 +- .../java/org/moonshot/config/CacheConfig.java | 35 +++++++++++++++++++ .../java/org/moonshot/config/RedisConfig.java | 8 ++++- .../service/UserPrincipalDetailsService.java | 2 +- .../user/repository/UserCustomRepository.java | 10 ++++++ .../repository/UserCustomRepositoryImpl.java | 25 +++++++++++++ .../user/repository/UserJpaRepository.java | 19 ++++++++++ .../user/repository/UserRepository.java | 17 +-------- 8 files changed, 99 insertions(+), 19 deletions(-) create mode 100644 moonshot-auth/src/main/java/org/moonshot/config/CacheConfig.java create mode 100644 moonshot-domain/src/main/java/org/moonshot/user/repository/UserCustomRepository.java create mode 100644 moonshot-domain/src/main/java/org/moonshot/user/repository/UserCustomRepositoryImpl.java create mode 100644 moonshot-domain/src/main/java/org/moonshot/user/repository/UserJpaRepository.java diff --git a/moonshot-api/src/main/java/org/moonshot/objective/service/ObjectiveService.java b/moonshot-api/src/main/java/org/moonshot/objective/service/ObjectiveService.java index ba304d2b..e4750a2e 100644 --- a/moonshot-api/src/main/java/org/moonshot/objective/service/ObjectiveService.java +++ b/moonshot-api/src/main/java/org/moonshot/objective/service/ObjectiveService.java @@ -46,7 +46,7 @@ public class ObjectiveService implements IndexService { private final ObjectiveRepository objectiveRepository; public void createObjective(final Long userId, final OKRCreateRequestDto request) { - User user = userRepository.findById(userId) + User user = userRepository.findByIdWithCache(userId) .orElseThrow(() -> new NotFoundException(NOT_FOUND_USER)); List objectives = objectiveRepository.findAllByUserId(userId); diff --git a/moonshot-auth/src/main/java/org/moonshot/config/CacheConfig.java b/moonshot-auth/src/main/java/org/moonshot/config/CacheConfig.java new file mode 100644 index 00000000..e6047516 --- /dev/null +++ b/moonshot-auth/src/main/java/org/moonshot/config/CacheConfig.java @@ -0,0 +1,35 @@ +package org.moonshot.config; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.time.Duration; +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.GenericJackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +@Configuration +@EnableCaching +@RequiredArgsConstructor +public class CacheConfig { + + private final RedisConnectionFactory redisConnectionFactory; + private final ObjectMapper objectMapper; + + @Bean + public CacheManager redisCacheManager() { + RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() + .serializeKeysWith(SerializationPair.fromSerializer(new StringRedisSerializer())) + .serializeValuesWith(SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())) + .entryTtl(Duration.ofSeconds(60 * 60)); + + return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory).cacheDefaults(redisCacheConfiguration).build(); + } + +} diff --git a/moonshot-auth/src/main/java/org/moonshot/config/RedisConfig.java b/moonshot-auth/src/main/java/org/moonshot/config/RedisConfig.java index 7cf704b9..1537aef2 100644 --- a/moonshot-auth/src/main/java/org/moonshot/config/RedisConfig.java +++ b/moonshot-auth/src/main/java/org/moonshot/config/RedisConfig.java @@ -1,5 +1,7 @@ package org.moonshot.config; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -8,10 +10,12 @@ import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; +import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration @EnableRedisRepositories +@RequiredArgsConstructor public class RedisConfig { @Value("${redis.host}") @@ -20,6 +24,8 @@ public class RedisConfig { @Value("${redis.port}") private int port; + private final ObjectMapper objectMapper; + @Bean public RedisConnectionFactory redisConnectionFactory() { return new LettuceConnectionFactory(host, port); @@ -30,7 +36,7 @@ public RedisConnectionFactory redisConnectionFactory() { public RedisTemplate redisTemplate() { RedisTemplate redisTemplate = new RedisTemplate<>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); - redisTemplate.setValueSerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer(objectMapper)); redisTemplate.setConnectionFactory(redisConnectionFactory()); return redisTemplate; } diff --git a/moonshot-auth/src/main/java/org/moonshot/security/service/UserPrincipalDetailsService.java b/moonshot-auth/src/main/java/org/moonshot/security/service/UserPrincipalDetailsService.java index 027d9d3a..ae4bc332 100644 --- a/moonshot-auth/src/main/java/org/moonshot/security/service/UserPrincipalDetailsService.java +++ b/moonshot-auth/src/main/java/org/moonshot/security/service/UserPrincipalDetailsService.java @@ -17,7 +17,7 @@ public class UserPrincipalDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { - return userRepository.findById(Long.parseLong(username)) + return userRepository.findByIdWithCache(Long.parseLong(username)) .map(UserPrincipal::new) .orElseThrow(UnauthorizedException::new); } diff --git a/moonshot-domain/src/main/java/org/moonshot/user/repository/UserCustomRepository.java b/moonshot-domain/src/main/java/org/moonshot/user/repository/UserCustomRepository.java new file mode 100644 index 00000000..9c877853 --- /dev/null +++ b/moonshot-domain/src/main/java/org/moonshot/user/repository/UserCustomRepository.java @@ -0,0 +1,10 @@ +package org.moonshot.user.repository; + +import java.util.Optional; +import org.moonshot.user.model.User; + +public interface UserCustomRepository { + + Optional findByIdWithCache(Long id); + +} diff --git a/moonshot-domain/src/main/java/org/moonshot/user/repository/UserCustomRepositoryImpl.java b/moonshot-domain/src/main/java/org/moonshot/user/repository/UserCustomRepositoryImpl.java new file mode 100644 index 00000000..47412189 --- /dev/null +++ b/moonshot-domain/src/main/java/org/moonshot/user/repository/UserCustomRepositoryImpl.java @@ -0,0 +1,25 @@ +package org.moonshot.user.repository; + +import static org.moonshot.user.model.QUser.user; + +import com.querydsl.jpa.impl.JPAQueryFactory; +import java.util.Optional; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.moonshot.user.model.User; +import org.springframework.cache.annotation.Cacheable; + +@Slf4j +@RequiredArgsConstructor +public class UserCustomRepositoryImpl implements UserCustomRepository { + + private final JPAQueryFactory queryFactory; + + @Override + @Cacheable(value = "user", cacheManager = "redisCacheManager") + public Optional findByIdWithCache(final Long id) { + log.info("id : " + id); + return Optional.ofNullable(queryFactory.selectFrom(user) + .where(user.id.eq(id)).fetchOne()); + } +} diff --git a/moonshot-domain/src/main/java/org/moonshot/user/repository/UserJpaRepository.java b/moonshot-domain/src/main/java/org/moonshot/user/repository/UserJpaRepository.java new file mode 100644 index 00000000..4dcf9ff3 --- /dev/null +++ b/moonshot-domain/src/main/java/org/moonshot/user/repository/UserJpaRepository.java @@ -0,0 +1,19 @@ +package org.moonshot.user.repository; + + +import java.time.LocalDateTime; +import java.util.List; +import java.util.Optional; +import org.moonshot.user.model.User; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +public interface UserJpaRepository extends JpaRepository { + + Optional findUserBySocialId(String socialId); + Optional findUserByNickname(String nickname); + @Query("SELECT u FROM User u WHERE u.deleteAt < :currentDate") + List findIdByDeletedAtBefore(LocalDateTime currentDate); + +} + diff --git a/moonshot-domain/src/main/java/org/moonshot/user/repository/UserRepository.java b/moonshot-domain/src/main/java/org/moonshot/user/repository/UserRepository.java index 2527f08d..3b463772 100644 --- a/moonshot-domain/src/main/java/org/moonshot/user/repository/UserRepository.java +++ b/moonshot-domain/src/main/java/org/moonshot/user/repository/UserRepository.java @@ -1,19 +1,4 @@ package org.moonshot.user.repository; - -import java.time.LocalDateTime; -import java.util.List; -import java.util.Optional; -import org.moonshot.user.model.User; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; - -public interface UserRepository extends JpaRepository { - - Optional findUserBySocialId(String socialId); - Optional findUserByNickname(String nickname); - @Query("SELECT u FROM User u WHERE u.deleteAt < :currentDate") - List findIdByDeletedAtBefore(LocalDateTime currentDate); - +public interface UserRepository extends UserJpaRepository, UserCustomRepository { } - From 3ea28c30f0624f7b54316dc783d5058a58d2ce26 Mon Sep 17 00:00:00 2001 From: its-sky Date: Thu, 11 Apr 2024 17:19:15 +0900 Subject: [PATCH 2/4] =?UTF-8?q?[Del]=20#263=20-=20=ED=95=84=EC=9A=94=20?= =?UTF-8?q?=EC=97=86=EB=8A=94=20=EB=A1=9C=EA=B9=85=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/moonshot/user/repository/UserCustomRepositoryImpl.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/moonshot-domain/src/main/java/org/moonshot/user/repository/UserCustomRepositoryImpl.java b/moonshot-domain/src/main/java/org/moonshot/user/repository/UserCustomRepositoryImpl.java index 47412189..487c225d 100644 --- a/moonshot-domain/src/main/java/org/moonshot/user/repository/UserCustomRepositoryImpl.java +++ b/moonshot-domain/src/main/java/org/moonshot/user/repository/UserCustomRepositoryImpl.java @@ -9,7 +9,6 @@ import org.moonshot.user.model.User; import org.springframework.cache.annotation.Cacheable; -@Slf4j @RequiredArgsConstructor public class UserCustomRepositoryImpl implements UserCustomRepository { @@ -18,7 +17,6 @@ public class UserCustomRepositoryImpl implements UserCustomRepository { @Override @Cacheable(value = "user", cacheManager = "redisCacheManager") public Optional findByIdWithCache(final Long id) { - log.info("id : " + id); return Optional.ofNullable(queryFactory.selectFrom(user) .where(user.id.eq(id)).fetchOne()); } From 94e7179aa8766639fcdfe6108667de82e6876614 Mon Sep 17 00:00:00 2001 From: its-sky Date: Thu, 11 Apr 2024 18:06:26 +0900 Subject: [PATCH 3/4] =?UTF-8?q?[Chore]=20#263=20-=20=EC=BB=A8=EB=B2=A4?= =?UTF-8?q?=EC=85=98=EC=97=90=20=EB=A7=9E=EA=B2=8C=20=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/moonshot/user/repository/UserCustomRepositoryImpl.java | 1 + 1 file changed, 1 insertion(+) diff --git a/moonshot-domain/src/main/java/org/moonshot/user/repository/UserCustomRepositoryImpl.java b/moonshot-domain/src/main/java/org/moonshot/user/repository/UserCustomRepositoryImpl.java index 487c225d..69c9f744 100644 --- a/moonshot-domain/src/main/java/org/moonshot/user/repository/UserCustomRepositoryImpl.java +++ b/moonshot-domain/src/main/java/org/moonshot/user/repository/UserCustomRepositoryImpl.java @@ -20,4 +20,5 @@ public Optional findByIdWithCache(final Long id) { return Optional.ofNullable(queryFactory.selectFrom(user) .where(user.id.eq(id)).fetchOne()); } + } From bb5d61ceb30cff13430635607cef734d89d7abce Mon Sep 17 00:00:00 2001 From: its-sky Date: Thu, 11 Apr 2024 18:24:12 +0900 Subject: [PATCH 4/4] =?UTF-8?q?[Chore]=20#263=20-=20=EC=BB=A8=EB=B2=A4?= =?UTF-8?q?=EC=85=98=EC=97=90=20=EB=A7=9E=EA=B2=8C=20=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/moonshot/user/repository/UserCustomRepositoryImpl.java | 1 - 1 file changed, 1 deletion(-) diff --git a/moonshot-domain/src/main/java/org/moonshot/user/repository/UserCustomRepositoryImpl.java b/moonshot-domain/src/main/java/org/moonshot/user/repository/UserCustomRepositoryImpl.java index 69c9f744..73c236ea 100644 --- a/moonshot-domain/src/main/java/org/moonshot/user/repository/UserCustomRepositoryImpl.java +++ b/moonshot-domain/src/main/java/org/moonshot/user/repository/UserCustomRepositoryImpl.java @@ -5,7 +5,6 @@ import com.querydsl.jpa.impl.JPAQueryFactory; import java.util.Optional; import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; import org.moonshot.user.model.User; import org.springframework.cache.annotation.Cacheable;