Skip to content

Commit b719ec9

Browse files
authored
fix: 조회수 한 시간 내 중복 조회 무시
fix: 조회수 한 시간 내 중복 조회 무시
2 parents ec8345a + b89f567 commit b719ec9

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff 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

6669
tasks.named('test') {

src/main/java/UMC_7th/Closit/domain/post/service/PostQueryServiceImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import UMC_7th.Closit.domain.user.entity.User;
1515
import UMC_7th.Closit.global.apiPayload.code.status.ErrorStatus;
1616
import UMC_7th.Closit.global.apiPayload.exception.GeneralException;
17+
import UMC_7th.Closit.global.infra.cache.ViewBlockCacheService;
1718
import UMC_7th.Closit.security.SecurityUtil;
1819
import lombok.RequiredArgsConstructor;
1920
import org.springframework.data.domain.Pageable;
@@ -29,6 +30,7 @@
2930
@RequiredArgsConstructor
3031
public 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);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

0 commit comments

Comments
 (0)