Skip to content

Commit

Permalink
[REFACTOR] 효율적으로 redis를 활용하기 및 클린코딩 (#191) (#192)
Browse files Browse the repository at this point in the history
* ♻️ refactor: 좋아요 및 댓글 관련 자원 redis TTL 일주일로 설정 (#191)

* ♻️ refactor: 공통된 로직 공통메서드로 클린코딩 (#191)
  • Loading branch information
jiiiiiw authored Sep 14, 2024
1 parent ac6e969 commit ebe0eb2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,23 @@ 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);

accountsCommonService.checkUserActivity(user);

// 프로필 존재 확인
accountsCommonService.checkUserProfile(user);
tacticRepository.save(tactic);
saveUserTactic(user, tactic);

// 11개의 개별 포지션에 대한 설명
createPositionDetails(request.positionDetails(), tactic);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);

Expand Down
23 changes: 16 additions & 7 deletions src/main/java/com/capstone/BnagFer/global/util/RedisUtil.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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) {
Expand All @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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) {
Expand All @@ -72,6 +80,7 @@ public String getFCMToken(String userEmail) {
return null;
}
}

public void removeFCMToken(String userEmail) {
redisTemplate.delete(userEmail);
}
Expand Down

0 comments on commit ebe0eb2

Please sign in to comment.