File tree Expand file tree Collapse file tree
src/main/java/UMC_7th/Closit Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -61,6 +61,9 @@ dependencies {
6161
6262 // redis
6363 implementation ' org.springframework.boot:spring-boot-starter-data-redis'
64+
65+ // caffeine
66+ implementation ' com.github.ben-manes.caffeine:caffeine:3.1.8'
6467}
6568
6669tasks. named(' test' ) {
Original file line number Diff line number Diff line change 1414import UMC_7th .Closit .domain .user .entity .User ;
1515import UMC_7th .Closit .global .apiPayload .code .status .ErrorStatus ;
1616import UMC_7th .Closit .global .apiPayload .exception .GeneralException ;
17+ import UMC_7th .Closit .global .infra .cache .ViewBlockCacheService ;
1718import UMC_7th .Closit .security .SecurityUtil ;
1819import lombok .RequiredArgsConstructor ;
1920import org .springframework .data .domain .Pageable ;
2930@ RequiredArgsConstructor
3031public class PostQueryServiceImpl implements PostQueryService {
3132
33+ private final ViewBlockCacheService viewBlockCacheService ;
3234 private final PostRepository postRepository ;
3335 private final LikeRepository likeRepository ;
3436 private final BookmarkRepository bookmarkRepository ;
@@ -43,13 +45,16 @@ public class PostQueryServiceImpl implements PostQueryService {
4345 @ Transactional // ← readOnly 제거 또는 false 설정
4446 public PostResponseDTO .PostPreviewDTO getPostById (Long postId ) {
4547 User currentUser = securityUtil .getCurrentUser ();
48+ Long userId = currentUser .getId ();
4649
4750 // 게시글 조회
4851 Post post = postRepository .findById (postId )
4952 .orElseThrow (() -> new GeneralException (ErrorStatus ._NOT_FOUND ));
5053
5154 // 조회수 증가
52- post .increaseView ();
55+ if (viewBlockCacheService .shouldIncreaseView (userId , postId )) {
56+ post .increaseView ();
57+ }
5358
5459 // 좋아요 여부 확인
5560 Boolean isLiked = likeRepository .existsByUserAndPost (currentUser , post );
Original file line number Diff line number Diff line change 1+ package UMC_7th .Closit .global .infra .cache ;
2+
3+ import com .github .benmanes .caffeine .cache .Cache ;
4+ import com .github .benmanes .caffeine .cache .Caffeine ;
5+ import org .springframework .stereotype .Service ;
6+ import java .util .concurrent .TimeUnit ;
7+
8+ @ Service
9+ public class ViewBlockCacheService {
10+ private final Cache <String , Boolean > viewCache = Caffeine .newBuilder ()
11+ .expireAfterWrite (1 , TimeUnit .HOURS ) // 1시간 동안 유지
12+ .maximumSize (100_000 ) // 최대 캐시 크기 설정
13+ .build ();
14+
15+ public boolean shouldIncreaseView (Long userId , Long postId ) {
16+ String key = generateKey (userId , postId );
17+ Boolean previous = viewCache .asMap ().putIfAbsent (key , true );
18+ return previous == null ; // 이전 값이 없으면 첫 조회
19+ }
20+
21+ private String generateKey (Long userId , Long postId ) {
22+ return userId + ":" + postId ;
23+ }
24+ }
You can’t perform that action at this time.
0 commit comments