-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[Refactor] #263 - User 데이터 캐싱
- Loading branch information
Showing
8 changed files
with
97 additions
and
19 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
35 changes: 35 additions & 0 deletions
35
moonshot-auth/src/main/java/org/moonshot/config/CacheConfig.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,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(); | ||
} | ||
|
||
} |
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
10 changes: 10 additions & 0 deletions
10
moonshot-domain/src/main/java/org/moonshot/user/repository/UserCustomRepository.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,10 @@ | ||
package org.moonshot.user.repository; | ||
|
||
import java.util.Optional; | ||
import org.moonshot.user.model.User; | ||
|
||
public interface UserCustomRepository { | ||
|
||
Optional<User> findByIdWithCache(Long id); | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
moonshot-domain/src/main/java/org/moonshot/user/repository/UserCustomRepositoryImpl.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,23 @@ | ||
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 org.moonshot.user.model.User; | ||
import org.springframework.cache.annotation.Cacheable; | ||
|
||
@RequiredArgsConstructor | ||
public class UserCustomRepositoryImpl implements UserCustomRepository { | ||
|
||
private final JPAQueryFactory queryFactory; | ||
|
||
@Override | ||
@Cacheable(value = "user", cacheManager = "redisCacheManager") | ||
public Optional<User> findByIdWithCache(final Long id) { | ||
return Optional.ofNullable(queryFactory.selectFrom(user) | ||
.where(user.id.eq(id)).fetchOne()); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
moonshot-domain/src/main/java/org/moonshot/user/repository/UserJpaRepository.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,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<User, Long> { | ||
|
||
Optional<User> findUserBySocialId(String socialId); | ||
Optional<User> findUserByNickname(String nickname); | ||
@Query("SELECT u FROM User u WHERE u.deleteAt < :currentDate") | ||
List<User> findIdByDeletedAtBefore(LocalDateTime currentDate); | ||
|
||
} | ||
|
17 changes: 1 addition & 16 deletions
17
moonshot-domain/src/main/java/org/moonshot/user/repository/UserRepository.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 |
---|---|---|
@@ -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<User, Long> { | ||
|
||
Optional<User> findUserBySocialId(String socialId); | ||
Optional<User> findUserByNickname(String nickname); | ||
@Query("SELECT u FROM User u WHERE u.deleteAt < :currentDate") | ||
List<User> findIdByDeletedAtBefore(LocalDateTime currentDate); | ||
|
||
public interface UserRepository extends UserJpaRepository, UserCustomRepository { | ||
} | ||
|