Skip to content

Commit e48226f

Browse files
Merge pull request #439 from Juinjang/feat/#434
[feat/#434] 조회수 버그 fix 및 로직 변경
2 parents 7f6119d + 7feface commit e48226f

File tree

9 files changed

+32
-133
lines changed

9 files changed

+32
-133
lines changed

src/main/java/umc/th/juinjang/JuinjangApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import org.springframework.scheduling.annotation.EnableScheduling;
1010

1111
@SpringBootApplication
12-
@EnableAsync
12+
// @EnableAsync
1313
@ImportAutoConfiguration({FeignAutoConfiguration.class})
1414
@EnableScheduling
1515
@EnableRetry

src/main/java/umc/th/juinjang/api/note/shared/service/SharedNoteQueryService.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import umc.th.juinjang.domain.note.shared.model.SharedNote;
4040
import umc.th.juinjang.domain.pencil.used.model.UsedPencil;
4141
import umc.th.juinjang.domain.report.model.Report;
42-
import umc.th.juinjang.event.publisher.ApplicationRewardViewCountPublisherAdapter;
4342

4443
@Service
4544
@Slf4j
@@ -51,17 +50,16 @@ public class SharedNoteQueryService {
5150
private final LikedNoteFinder likedNoteFinder;
5251
private final ChecklistAnswerFinder checklistAnswerFinder;
5352
private final ViewCountService viewCountService;
54-
private final ApplicationRewardViewCountPublisherAdapter applicationRewardViewCountPublisherAdapter;
5553
private final ReportFinder reportFinder;
5654

57-
@Transactional(readOnly = true)
55+
@Transactional
5856
public SharedNoteGetResponse findSharedNote(Member member, Long sharedNoteId) {
5957
SharedNote sharedNote = sharedNoteFinder.findByIdWithNoteAndAddress(sharedNoteId);
6058
Limjang limjang = sharedNote.getLimjang();
6159

6260
boolean isBuyerOrOwner = getIsBuyerOrOwner(member, sharedNote);
6361

64-
long viewCount = getViewCountAndCheckReward(member, sharedNoteId, sharedNote);
62+
long viewCount = viewCountService.getViewCount(member, sharedNote);
6563

6664
Integer countBuyer = makeBuyerCount(usedPencilFinder.countBySharedNoteId(sharedNoteId));
6765
boolean isLiked = likedNoteFinder.existsByMemberAndSharedNote(member, sharedNote);
@@ -80,19 +78,6 @@ private boolean getIsBuyerOrOwner(Member requestMember, SharedNote sharedNote) {
8078
sharedNote.getMember().getMemberId().equals(requestMember.getMemberId());
8179
}
8280

83-
private long getViewCountAndCheckReward(Member member, Long sharedNoteId, SharedNote sharedNote) {
84-
long viewCount = viewCountService.getRedisViewCount(sharedNote.getSharedNoteId());
85-
if (!viewCountService.isDuplicate(member.getMemberId(), sharedNoteId)) {
86-
viewCountService.increaseViewCount(sharedNoteId);
87-
viewCount++;
88-
viewCountService.recordViewerHistory(member.getMemberId(), sharedNoteId);
89-
90-
applicationRewardViewCountPublisherAdapter.checkViewCountRewardPolicy(sharedNote.getMember(),
91-
sharedNote.getSharedNoteId(), viewCount);
92-
}
93-
return viewCount;
94-
}
95-
9681
private Integer makeBuyerCount(int count) {
9782
if (count >= 100) {
9883
return 100;
@@ -128,7 +113,7 @@ public SharedNoteExploreGetResponse findExploreSharedNote(Member member, List<St
128113
private Map<Long, Long> mapIdsAndViewcount(List<SharedNote> sharedNotes) {
129114
return sharedNotes.stream().collect(Collectors.toMap(
130115
SharedNote::getSharedNoteId,
131-
it -> viewCountService.getRedisViewCount(it.getSharedNoteId())
116+
SharedNote::getViewCount
132117
));
133118
}
134119

src/main/java/umc/th/juinjang/api/note/shared/service/SharedNoteUpdater.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public class SharedNoteUpdater {
1212

1313
private final SharedNoteRepository sharedNoteRepository;
1414

15-
void updateViewCount(long sharedNoteId, long addAmount) {
16-
sharedNoteRepository.incrementViewCount(sharedNoteId, addAmount);
15+
public void updateViewCount(long sharedNoteId) {
16+
sharedNoteRepository.incrementViewCount(sharedNoteId);
1717
}
1818

1919
public void incrementLikedCountById(Long sharedNoteId) {

src/main/java/umc/th/juinjang/api/note/shared/service/ViewCountService.java

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import lombok.RequiredArgsConstructor;
1111
import lombok.extern.slf4j.Slf4j;
1212
import umc.th.juinjang.common.redis.RedisKeyFactory;
13+
import umc.th.juinjang.domain.member.model.Member;
14+
import umc.th.juinjang.domain.note.shared.model.SharedNote;
15+
import umc.th.juinjang.event.publisher.ApplicationRewardViewCountPublisherAdapter;
1316

1417
@Component
1518
@RequiredArgsConstructor
@@ -18,6 +21,8 @@ public class ViewCountService {
1821

1922
private final RedisTemplate<String, String> redisTemplate;
2023
private final SharedNoteFinder sharedNoteFinder;
24+
private final SharedNoteUpdater sharedNoteUpdater;
25+
private final ApplicationRewardViewCountPublisherAdapter applicationRewardViewCountPublisherAdapter;
2126

2227
public void recordViewerHistory(long memberId, long sharedNoteId) {
2328
try {
@@ -28,14 +33,6 @@ public void recordViewerHistory(long memberId, long sharedNoteId) {
2833
}
2934
}
3035

31-
public void increaseViewCount(long sharedNoteId) {
32-
try {
33-
redisTemplate.opsForValue().increment(RedisKeyFactory.viewCountKey(sharedNoteId));
34-
} catch (RedisConnectionFailureException | RedisSystemException e) {
35-
log.error("Redis 조회수 증가 실패, sharedNoteId={}", sharedNoteId, e);
36-
}
37-
}
38-
3936
public boolean isDuplicate(long memberId, long sharedNoteId) {
4037
try {
4138
return Boolean.TRUE.equals(redisTemplate.hasKey(RedisKeyFactory.viewHistoryKey(sharedNoteId, memberId)));
@@ -45,29 +42,18 @@ public boolean isDuplicate(long memberId, long sharedNoteId) {
4542
}
4643
}
4744

48-
public Long getRedisViewCount(long sharedNoteId) {
45+
public long getViewCount(Member member, SharedNote sharedNote) {
46+
long sharedNoteId = sharedNote.getSharedNoteId();
47+
long viewCount = sharedNoteFinder.findViewCountById(sharedNoteId);
4948

50-
String key = RedisKeyFactory.viewCountKey(sharedNoteId);
49+
if (!isDuplicate(member.getMemberId(), sharedNoteId) && sharedNote.getDeletedAt() == null) {
50+
sharedNoteUpdater.updateViewCount(sharedNoteId);
51+
viewCount++;
52+
recordViewerHistory(member.getMemberId(), sharedNoteId);
5153

52-
try {
53-
Object value = redisTemplate.opsForValue().get(key);
54-
55-
if (value == null) {
56-
Long viewCountFromDb = sharedNoteFinder.findViewCountById(sharedNoteId);
57-
58-
if (viewCountFromDb == null) {
59-
log.warn("DB의 sharedNote 조회수가 null sharedNoteId={}", sharedNoteId);
60-
viewCountFromDb = 0L;
61-
}
62-
63-
redisTemplate.opsForValue().set(key, viewCountFromDb.toString());
64-
return viewCountFromDb;
65-
}
66-
67-
return Long.parseLong(value.toString());
68-
} catch (RedisConnectionFailureException | RedisSystemException e) {
69-
log.error("Redis 장애 발생, 기본값 반환 sharedNoteID={}", sharedNoteId, e);
70-
return 0L;
54+
applicationRewardViewCountPublisherAdapter.checkViewCountRewardPolicy(sharedNote.getMember(),
55+
sharedNote.getSharedNoteId(), viewCount);
7156
}
57+
return viewCount;
7258
}
7359
}

src/main/java/umc/th/juinjang/api/note/shared/service/ViewCountSyncScheduler.java

Lines changed: 0 additions & 76 deletions
This file was deleted.

src/main/java/umc/th/juinjang/api/reward/service/RewardService.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package umc.th.juinjang.api.reward.service;
22

33
import org.springframework.stereotype.Component;
4+
import org.springframework.transaction.annotation.Propagation;
45
import org.springframework.transaction.annotation.Transactional;
56

67
import lombok.RequiredArgsConstructor;
8+
import lombok.extern.slf4j.Slf4j;
79
import umc.th.juinjang.api.pencil.service.AcquiredPencilUpdater;
810
import umc.th.juinjang.api.pencilAccount.service.PencilAccountFinder;
911
import umc.th.juinjang.domain.member.model.Member;
@@ -16,6 +18,7 @@
1618

1719
@Component
1820
@RequiredArgsConstructor
21+
@Slf4j
1922
public class RewardService {
2023

2124
private final AcquiredPencilUpdater acquiredPencilUpdater;
@@ -24,7 +27,7 @@ public class RewardService {
2427
private final RewardFinder rewardFinder;
2528
private final RewardUpdater rewardUpdater;
2629

27-
@Transactional
30+
@Transactional(propagation = Propagation.REQUIRES_NEW)
2831
public void giveViewCountReward(Member member, Long sharedNoteId, Long milestone, Long rewardPencil) {
2932

3033
if (alreadyViewCountRewardEarned(RewardType.VIEWCOUNT, sharedNoteId, milestone)) {
@@ -37,6 +40,9 @@ public void giveViewCountReward(Member member, Long sharedNoteId, Long milestone
3740
acquiredPencilUpdater.save(
3841
createAcquiredPencil(member, sharedNoteId, milestone, rewardPencil));
3942
rewardUpdater.save(createReward(member, sharedNoteId, milestone, rewardPencil));
43+
44+
log.info("유저에게 조회수 리워드 지급 완료: memberId={}, sharedNoteId={}, milestone={}, rewardPencil={}",
45+
member.getMemberId(), sharedNoteId, milestone, rewardPencil);
4046
}
4147

4248
private AcquiredPencil createAcquiredPencil(Member member, Long sharedNoteId, Long milestone, Long rewardPencil) {

src/main/java/umc/th/juinjang/domain/note/shared/model/SharedNote.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ public void updateDeletedAt(Timestamp deletedAt) {
9797
this.deletedAt = deletedAt;
9898
}
9999

100-
// 23년 12월 초반 임장
101100
public String getPullPeriod() {
102101
String shortYear = String.valueOf(this.year).substring(2);
103102
return shortYear + "년 " + this.month + "월 " + this.period + " 임장";

src/main/java/umc/th/juinjang/domain/note/shared/repository/SharedNoteRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public interface SharedNoteRepository extends JpaRepository<SharedNote, Long>, S
1919
Optional<SharedNote> findByIdWithNoteAndAddress(@Param("sharedNoteId") Long sharedNoteId);
2020

2121
@Modifying
22-
@Query("UPDATE SharedNote s SET s.viewCount = :updateViewCount WHERE s.sharedNoteId = :sharedNoteId")
23-
void incrementViewCount(@Param("sharedNoteId") Long sharedNoteId, @Param("updateViewCount") Long updateViewCount);
22+
@Query("UPDATE SharedNote s SET s.viewCount = s.viewCount + 1 WHERE s.sharedNoteId = :sharedNoteId")
23+
void incrementViewCount(@Param("sharedNoteId") Long sharedNoteId);
2424

2525
@Modifying
2626
@Query("UPDATE SharedNote sn SET sn.likeCount = sn.likeCount + 1 WHERE sn.sharedNoteId = :sharedNoteId")

src/main/java/umc/th/juinjang/event/subscriber/RewardViewCountEventListener.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ public class RewardViewCountEventListener {
1919
private final ViewCountPolicy viewCountPolicy;
2020
private final RewardService rewardService;
2121

22-
@Async
2322
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
23+
@Async
2424
public void handleRewardViewCountEvent(RewardViewCountEvent rewardViewCountEvent) {
2525

26-
Long reward = viewCountPolicy.getRewardForExactMilestone(
27-
rewardViewCountEvent.viewCount());
26+
Long reward = viewCountPolicy.getRewardForExactMilestone(rewardViewCountEvent.viewCount());
2827

2928
if (reward == null) {
3029
return;

0 commit comments

Comments
 (0)