-
Notifications
You must be signed in to change notification settings - Fork 8
fix: 게시글 중복 생성 방지 #649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: 게시글 중복 생성 방지 #649
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package com.example.solidconnection.community.post.service; | ||
|
|
||
| import static com.example.solidconnection.redis.RedisConstants.POST_CREATE_PREFIX; | ||
| import static com.example.solidconnection.redis.RedisConstants.VALIDATE_POST_CREATE_TTL; | ||
| import static com.example.solidconnection.redis.RedisConstants.VALIDATE_VIEW_COUNT_KEY_PREFIX; | ||
| import static com.example.solidconnection.redis.RedisConstants.VALIDATE_VIEW_COUNT_TTL; | ||
| import static com.example.solidconnection.redis.RedisConstants.VIEW_COUNT_KEY_PREFIX; | ||
|
|
||
| import com.example.solidconnection.redis.RedisService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class PostRedisManager { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 흩어진 |
||
|
|
||
| private final RedisService redisService; | ||
|
|
||
| public Long getPostIdFromPostViewCountRedisKey(String key) { | ||
| return Long.parseLong(key.substring(VIEW_COUNT_KEY_PREFIX.getValue().length())); | ||
| } | ||
|
|
||
| public Long getAndDeleteViewCount(String key) { | ||
| return redisService.getAndDelete(key); | ||
| } | ||
|
|
||
| public void deleteViewCountCache(Long postId) { | ||
| String key = getPostViewCountRedisKey(postId); | ||
| redisService.deleteKey(key); | ||
| } | ||
|
|
||
| public void incrementViewCountIfFirstAccess(long siteUserId, Long postId) { | ||
| String validateKey = getValidatePostViewCountRedisKey(siteUserId, postId); | ||
| boolean isFirstAccess = redisService.isPresent(validateKey, VALIDATE_VIEW_COUNT_TTL.getValue()); | ||
|
|
||
| if (isFirstAccess) { | ||
| String viewCountKey = getPostViewCountRedisKey(postId); | ||
| redisService.increaseViewCount(viewCountKey); | ||
| } | ||
| } | ||
|
|
||
| public String getPostViewCountRedisKey(Long postId) { | ||
| return VIEW_COUNT_KEY_PREFIX.getValue() + postId; | ||
| } | ||
|
|
||
| public String getValidatePostViewCountRedisKey(long siteUserId, Long postId) { | ||
| return VALIDATE_VIEW_COUNT_KEY_PREFIX.getValue() + postId + ":" + siteUserId; | ||
| } | ||
|
|
||
| public boolean isPostCreationAllowed(Long siteUserId) { | ||
| String key = getPostCreateRedisKey(siteUserId); | ||
| return redisService.isPresent(key, VALIDATE_POST_CREATE_TTL.getValue()); | ||
| } | ||
|
|
||
| public String getPostCreateRedisKey(Long siteUserId) { | ||
| return POST_CREATE_PREFIX.getValue() + siteUserId; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,7 +2,6 @@ | |||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| import com.example.solidconnection.community.post.domain.Post; | ||||||||||||||||||||||||||||||||
| import com.example.solidconnection.community.post.repository.PostRepository; | ||||||||||||||||||||||||||||||||
| import com.example.solidconnection.util.RedisUtils; | ||||||||||||||||||||||||||||||||
| import lombok.RequiredArgsConstructor; | ||||||||||||||||||||||||||||||||
| import lombok.extern.slf4j.Slf4j; | ||||||||||||||||||||||||||||||||
| import org.springframework.scheduling.annotation.Async; | ||||||||||||||||||||||||||||||||
|
|
@@ -17,14 +16,13 @@ | |||||||||||||||||||||||||||||||
| public class UpdateViewCountService { | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| private final PostRepository postRepository; | ||||||||||||||||||||||||||||||||
| private final RedisService redisService; | ||||||||||||||||||||||||||||||||
| private final RedisUtils redisUtils; | ||||||||||||||||||||||||||||||||
| private final PostRedisManager postRedisManager; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| @Transactional | ||||||||||||||||||||||||||||||||
| @Async | ||||||||||||||||||||||||||||||||
| public void updateViewCount(String key) { | ||||||||||||||||||||||||||||||||
| Long postId = redisUtils.getPostIdFromPostViewCountRedisKey(key); | ||||||||||||||||||||||||||||||||
| Post post = postRepository.getById(postId); | ||||||||||||||||||||||||||||||||
| postRepository.increaseViewCount(postId, redisService.getAndDelete(key)); | ||||||||||||||||||||||||||||||||
| Long postId = postRedisManager.getPostIdFromPostViewCountRedisKey(key); | ||||||||||||||||||||||||||||||||
| Long viewCount = postRedisManager.getAndDeleteViewCount(key); | ||||||||||||||||||||||||||||||||
| postRepository.increaseViewCount(postId, viewCount); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
23
to
27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # Find the UpdateViewCountService file
find . -name "UpdateViewCountService.java" -type fRepository: solid-connection/solid-connect-server Length of output: 175 🏁 Script executed: # If found, read the full file with line numbers
cat -n src/main/java/com/example/solidconnection/community/post/service/UpdateViewCountService.java 2>/dev/null || echo "File not found, searching for it..."Repository: solid-connection/solid-connect-server Length of output: 1331 🏁 Script executed: # Search for PostRedisManager to understand getAndDeleteViewCount
rg -n 'class PostRedisManager' -A 50 --type=javaRepository: solid-connection/solid-connect-server Length of output: 5607 🏁 Script executed: # Search for getAndDeleteViewCount implementation
rg -n 'getAndDeleteViewCount' --type=java -B 3 -A 10Repository: solid-connection/solid-connect-server Length of output: 2848 🏁 Script executed: # Search for increaseViewCount implementation
rg -n 'increaseViewCount' --type=java -B 3 -A 10Repository: solid-connection/solid-connect-server Length of output: 13808 불필요한 DB 조회 제거 및 null 안전성 처리가 필요합니다. 아래 두 가지 개선 사항을 적용하세요:
수정 제안 `@Transactional`
`@Async`
public void updateViewCount(String key) {
Long postId = postRedisManager.getPostIdFromPostViewCountRedisKey(key);
- Post post = postRepository.getById(postId);
Long viewCount = postRedisManager.getAndDeleteViewCount(key);
- postRepository.increaseViewCount(postId, viewCount);
+ if (viewCount != null && viewCount > 0) {
+ postRepository.increaseViewCount(postId, viewCount);
+ }
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
fd "PostRedisManager.java" -t f --exec cat {}Repository: solid-connection/solid-connect-server
Length of output: 2330
🏁 Script executed:
fd "PostCommandService.java" -t f --exec cat {}Repository: solid-connection/solid-connect-server
Length of output: 6411
🏁 Script executed:
fd "RedisService.java" -t f --exec cat {}Repository: solid-connection/solid-connect-server
Length of output: 1878
🏁 Script executed:
fd "RedisConstants.java" -t f --exec cat {}Repository: solid-connection/solid-connect-server
Length of output: 789
생성 실패 시 Redis 락이 해제되지 않는 문제를 수정하세요.
문제의 원인
isPostCreationAllowed()는 Redis의 SETNX(setIfAbsent) 명령으로 5초 TTL의 락을 설정합니다.true를 반환하고, 이후 Board 조회·Post 저장·S3 이미지 업로드가 진행됩니다.사용자 영향
수정 방향
try-catch블록으로 감싸거나,PostRedisManager에 락 해제 메서드를 추가하는 방식을 권장합니다.🛠️ 제안하는 구현 예시
// 중복 생성 방지 if (!postRedisManager.isPostCreationAllowed(siteUserId)) { throw new CustomException(DUPLICATE_POST_CREATE_REQUEST); } - // 객체 생성 - Board board = boardRepository.getByCode(postCreateRequest.boardCode()); - Post post = postCreateRequest.toEntity(siteUser, board); - - // 이미지 처리 - savePostImages(imageFile, post); - Post createdPost = postRepository.save(post); - - return PostCreateResponse.from(createdPost); + try { + // 객체 생성 + Board board = boardRepository.getByCode(postCreateRequest.boardCode()); + Post post = postCreateRequest.toEntity(siteUser, board); + + // 이미지 처리 + savePostImages(imageFile, post); + Post createdPost = postRepository.save(post); + + return PostCreateResponse.from(createdPost); + } catch (Exception e) { + postRedisManager.releasePostCreationLock(siteUserId); + throw e; + }PostRedisManager에 다음 메서드를 추가하세요:🤖 Prompt for AI Agents