-
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
base: develop
Are you sure you want to change the base?
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 { | ||
|
|
||
| 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,14 @@ | |||||||||||||||||||||||||||||||
| 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); | ||||||||||||||||||||||||||||||||
| Long postId = postRedisManager.getPostIdFromPostViewCountRedisKey(key); | ||||||||||||||||||||||||||||||||
| Post post = postRepository.getById(postId); | ||||||||||||||||||||||||||||||||
| postRepository.increaseViewCount(postId, redisService.getAndDelete(key)); | ||||||||||||||||||||||||||||||||
| Long viewCount = postRedisManager.getAndDeleteViewCount(key); | ||||||||||||||||||||||||||||||||
| postRepository.increaseViewCount(postId, viewCount); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
23
to
28
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 |
||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| package com.example.solidconnection.community.post.service; | ||
| package com.example.solidconnection.redis; | ||
|
|
||
| import static com.example.solidconnection.community.post.service.RedisConstants.VALIDATE_VIEW_COUNT_TTL; | ||
| import static com.example.solidconnection.community.post.service.RedisConstants.VIEW_COUNT_TTL; | ||
| import static com.example.solidconnection.redis.RedisConstants.VALIDATE_VIEW_COUNT_TTL; | ||
| import static com.example.solidconnection.redis.RedisConstants.VIEW_COUNT_TTL; | ||
|
Comment on lines
+3
to
+4
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: #!/bin/bash
# RedisService.java에서 두 상수의 실제 사용 여부 확인
echo "=== VALIDATE_VIEW_COUNT_TTL 사용 확인 ==="
fd "RedisService.java" --type f -x grep -n "VALIDATE_VIEW_COUNT_TTL" {}
echo -e "\n=== VIEW_COUNT_TTL 사용 확인 ==="
fd "RedisService.java" --type f -x grep -n "VIEW_COUNT_TTL" {}
echo -e "\n=== 전체 import 문 확인 ==="
fd "RedisService.java" --type f -x head -20 {}Repository: solid-connection/solid-connect-server Length of output: 1325
확인 결과:
3번째 줄의 🤖 Prompt for AI Agents |
||
|
|
||
| import java.util.Collections; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
@@ -37,9 +37,9 @@ public Long getAndDelete(String key) { | |
| return Long.valueOf(redisTemplate.opsForValue().getAndDelete(key)); | ||
| } | ||
|
|
||
| public boolean isPresent(String key) { | ||
| public boolean isPresent(String key, String ttl) { | ||
| return Boolean.TRUE.equals(redisTemplate.opsForValue() | ||
| .setIfAbsent(key, "1", Long.parseLong(VALIDATE_VIEW_COUNT_TTL.getValue()), TimeUnit.SECONDS)); | ||
| .setIfAbsent(key, "1", Long.parseLong(ttl), TimeUnit.SECONDS)); | ||
| } | ||
|
|
||
| public boolean isKeyExists(String key) { | ||
|
|
||
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.
흩어진
Post관련 레디스 메서드 한 곳으로 통합하는 거 좋은 거 같아요 !