Skip to content

Commit

Permalink
Merge pull request #48 from lotteon2/hotfix/sns-postlike
Browse files Browse the repository at this point in the history
[FIX] fix postlike count
  • Loading branch information
ssjy4974 authored Jan 18, 2024
2 parents e2898ed + 48f8df6 commit c83bf4d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,12 @@ public List<Top4OOTDResponse> getTop4OOTDPosts(Long productId) {
public void addViewCount(Long id) {
Post post = postRepository.findByIdAndIsDeletedFalse(id);
try {
PostCountVO postCountVO =
new PostCountVO(post.getViewCount() + 1, post.getLikeCount(), post.getCommentCount());
PostCountVO dbPostCountVO =
new PostCountVO(post.getViewCount(), post.getLikeCount(), post.getCommentCount());
PostCountVO cachedPostCountVO = postCountRedisRepository.findOrPutPostCountVO(String.valueOf(id), dbPostCountVO);
cachedPostCountVO.addViewCount(1);
// update view count to cache
postCountRedisRepository.modifyPostCountVOAboutLikeCount(String.valueOf(id), postCountVO);
postCountRedisRepository.modifyPostCountVOAboutLikeCount(String.valueOf(id), cachedPostCountVO);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -315,12 +317,10 @@ public PostDetailResponse findDetailByIdWithIsFollowing(Long id, Long memberId)
postCountRedisRepository.findOrPutPostCountVO(
String.valueOf(postDetailResponse.getId()), dbPostCountVO);

// cache hit 로 인해서 db와 cache의 내용이 서로 다르다면 response를 업데이트
if (!dbPostCountVO.equals(cachedPostCountVO)) {
postDetailResponse.setViewCount(cachedPostCountVO.getViewCount());
postDetailResponse.setLikeCount(cachedPostCountVO.getLikeCount());
postDetailResponse.setCommentCount(cachedPostCountVO.getCommentCount());
}
postDetailResponse.setViewCount(cachedPostCountVO.getViewCount());
postDetailResponse.setLikeCount(cachedPostCountVO.getLikeCount());
postDetailResponse.setCommentCount(cachedPostCountVO.getCommentCount());

} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void togglePostLike(Long memberId, List<Long> postIds) {
cachedPostCountVO.addLikeCount(countMap.get(post.getId()));
// update like count to cache
postCountRedisRepository.modifyPostCountVOAboutLikeCount(
String.valueOf(post.getId()), cachedPostCountVO);
String.valueOf(post.getId()), cachedPostCountVO);
}
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/dailyon/snsservice/vo/PostCountVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ public void addLikeCount(Integer count) {
public void addCommentCount(Integer count) {
this.commentCount += count;
}

public void addViewCount(Integer count) {
this.viewCount += count;
}
}

0 comments on commit c83bf4d

Please sign in to comment.