-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KKUMI-118 [FEATURE] #73 포스트 좋아요 개수 조회 + 좋아요 Redis 캐싱
포스트에 좋아요 개수를 조회하는 부분 구현, 한번 조회됐던 좋아요개수는 30초간 캐싱되도록 함 흐름은 아래와 같습니다. 1. Redis에서 post의 좋아요 개수 조회 2-1. Redis에 존재하면 바로 리턴 2-2. Redis에 존재하지 않으면 DB에서 조회 후 PostLikeCountCacheMissEvent 발생시켜 Redis에 캐싱되도록 함
- Loading branch information
Showing
4 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/com/swmarastro/mykkumiserver/post/event/PostLikeCountCacheMissEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.swmarastro.mykkumiserver.post.event; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class PostLikeCountCacheMissEvent { | ||
|
||
private final Long postId; | ||
private final Long likeCount; | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/swmarastro/mykkumiserver/post/event/listener/PostLikeEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.swmarastro.mykkumiserver.post.event.listener; | ||
|
||
import com.swmarastro.mykkumiserver.global.util.RedisUtils; | ||
import com.swmarastro.mykkumiserver.post.event.PostLikeCountCacheMissEvent; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.time.Duration; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class PostLikeEventListener { | ||
|
||
private final RedisUtils redisUtils; | ||
private static final String POST_LIKE_KEY_PREFIX = "post:"; | ||
private static final String POST_LIKE_KEY_SUFFIX = ":likes"; | ||
private static final Duration POST_LIKE_DURATION = Duration.ofSeconds(30); | ||
|
||
@Async | ||
@EventListener | ||
public void setPostLikeCountCache(PostLikeCountCacheMissEvent postLikeCountCacheMissEvent) { | ||
String key = makePostLikeKey(postLikeCountCacheMissEvent.getPostId()); | ||
redisUtils.setValues(key, postLikeCountCacheMissEvent.getLikeCount(), POST_LIKE_DURATION); | ||
} | ||
|
||
private String makePostLikeKey(Long postId) { | ||
return POST_LIKE_KEY_PREFIX + postId + POST_LIKE_KEY_SUFFIX; | ||
} | ||
} |