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); }