From ebe0eb21693b7fb9310a2561259593ca00e6de57 Mon Sep 17 00:00:00 2001 From: jiiiiiw <98935125+jiiiiiw@users.noreply.github.com> Date: Sat, 14 Sep 2024 19:07:50 +0900 Subject: [PATCH] =?UTF-8?q?[REFACTOR]=20=ED=9A=A8=EC=9C=A8=EC=A0=81?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20redis=EB=A5=BC=20=ED=99=9C=EC=9A=A9?= =?UTF-8?q?=ED=95=98=EA=B8=B0=20=EB=B0=8F=20=ED=81=B4=EB=A6=B0=EC=BD=94?= =?UTF-8?q?=EB=94=A9=20(#191)=20(#192)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ♻️ refactor: 좋아요 및 댓글 관련 자원 redis TTL 일주일로 설정 (#191) * ♻️ refactor: 공통된 로직 공통메서드로 클린코딩 (#191) --- .../domain/tactic/service/TacticService.java | 18 ++++++++++----- .../BnagFer/global/util/RedisUtil.java | 23 +++++++++++++------ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/capstone/BnagFer/domain/tactic/service/TacticService.java b/src/main/java/com/capstone/BnagFer/domain/tactic/service/TacticService.java index 20e173c8..a4e87e67 100644 --- a/src/main/java/com/capstone/BnagFer/domain/tactic/service/TacticService.java +++ b/src/main/java/com/capstone/BnagFer/domain/tactic/service/TacticService.java @@ -36,6 +36,15 @@ public class TacticService { private final LikeRepository likeRepository; private final ApplicationEventPublisher eventPublisher; + private void saveUserTactic(User user, Object tacticOrComment) { + accountsCommonService.checkUserProfile(user); + if (tacticOrComment instanceof Tactic) { + tacticRepository.save((Tactic) tacticOrComment); + } else if (tacticOrComment instanceof TacticComment) { + commentRepository.save((TacticComment) tacticOrComment); + } + } + public TacticResponse createTactic(TacticCreateRequest request, User user){ Tactic tactic = request.toEntity(user); @@ -43,8 +52,7 @@ public TacticResponse createTactic(TacticCreateRequest request, User user){ accountsCommonService.checkUserActivity(user); // 프로필 존재 확인 - accountsCommonService.checkUserProfile(user); - tacticRepository.save(tactic); + saveUserTactic(user, tactic); // 11개의 개별 포지션에 대한 설명 createPositionDetails(request.positionDetails(), tactic); @@ -101,8 +109,7 @@ public TacticResponse copyTactic(Long tacticId, User user) { copyTactic.setCopyDetail(user, tactic); // 프로필 존재 확인 - accountsCommonService.checkUserProfile(user); - tacticRepository.save(copyTactic); + saveUserTactic(user, copyTactic); return TacticResponse.from(copyTactic); } @@ -123,8 +130,7 @@ public CommentResponse createComment(Long tacticId, CommentCreateRequest request TacticComment tacticComment = request.toEntity(user, tactic, parent); // 프로필 존재 확인 - accountsCommonService.checkUserProfile(user); - commentRepository.save(tacticComment); + saveUserTactic(user, tacticComment); commentCount++; redisUtil.saveCommentCount(tacticId, commentCount); diff --git a/src/main/java/com/capstone/BnagFer/global/util/RedisUtil.java b/src/main/java/com/capstone/BnagFer/global/util/RedisUtil.java index 6cf71761..ef1ace2d 100644 --- a/src/main/java/com/capstone/BnagFer/global/util/RedisUtil.java +++ b/src/main/java/com/capstone/BnagFer/global/util/RedisUtil.java @@ -1,19 +1,26 @@ package com.capstone.BnagFer.global.util; + import lombok.RequiredArgsConstructor; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; + @Component @RequiredArgsConstructor public class RedisUtil { private final RedisTemplate redisTemplate; + private static final long ONE_WEEK_IN_SECONDS = 7 * 24 * 60 * 60; // 일주일을 초로 표현 public void save(String key, Object val, Long time, TimeUnit timeUnit) { redisTemplate.opsForValue().set(key, val, time, timeUnit); } + private void saveWithOneWeekTTL(String key, Object val) { + redisTemplate.opsForValue().set(key, val, ONE_WEEK_IN_SECONDS, TimeUnit.SECONDS); + } + public void saveLikeCount(Long tacticId, Long likeCount) { - redisTemplate.opsForValue().set("tactic:" + tacticId + ":likeCount", likeCount.toString()); + saveWithOneWeekTTL("tactic:" + tacticId + ":likeCount", likeCount.toString()); } public Long getLikeCount(Long tacticId) { @@ -22,7 +29,7 @@ public Long getLikeCount(Long tacticId) { } public void boardSaveLikeCount(Long boardId, Long likeCount) { - redisTemplate.opsForValue().set("board:" + boardId + ":likeCount", likeCount.toString()); + saveWithOneWeekTTL("board:" + boardId + ":likeCount", likeCount.toString()); } public Long boardGetLikeCount(Long boardId) { @@ -31,19 +38,19 @@ public Long boardGetLikeCount(Long boardId) { } public void boardSaveCommentCount(Long boardId, Long commentCount) { - redisTemplate.opsForValue().set("board:" + boardId + ":commentCount", commentCount.toString()); + saveWithOneWeekTTL("board:" + boardId + ":commentCount", commentCount.toString()); } - public Long boardGetCommentCount(Long boardId){ + public Long boardGetCommentCount(Long boardId) { String commentCountStr = (String) redisTemplate.opsForValue().get("board:" + boardId + ":commentCount"); return commentCountStr != null ? Long.valueOf(commentCountStr) : null; } - public void saveCommentCount(Long tacticId, Long commentCount){ - redisTemplate.opsForValue().set("tactic:" + tacticId + ":commentCount", commentCount.toString()); + public void saveCommentCount(Long tacticId, Long commentCount) { + saveWithOneWeekTTL("tactic:" + tacticId + ":commentCount", commentCount.toString()); } - public Long getCommentCount(Long tacticId){ + public Long getCommentCount(Long tacticId) { String commentCountStr = (String) redisTemplate.opsForValue().get("tactic:" + tacticId + ":commentCount"); return commentCountStr != null ? Long.valueOf(commentCountStr) : null; } @@ -64,6 +71,7 @@ public void saveFCMToken(String userEmail, String fcmToken) { redisTemplate.opsForValue().set(userEmail, fcmToken); redisTemplate.expire(userEmail, 30, TimeUnit.DAYS); } + public String getFCMToken(String userEmail) { Object tokenObj = redisTemplate.opsForValue().get(userEmail); if (tokenObj != null) { @@ -72,6 +80,7 @@ public String getFCMToken(String userEmail) { return null; } } + public void removeFCMToken(String userEmail) { redisTemplate.delete(userEmail); }