-
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 |
|---|---|---|
| @@ -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.
🧩 Analysis chain
🏁 Script executed:
Repository: solid-connection/solid-connect-server
Length of output: 175
🏁 Script executed:
Repository: solid-connection/solid-connect-server
Length of output: 1331
🏁 Script executed:
Repository: solid-connection/solid-connect-server
Length of output: 5607
🏁 Script executed:
Repository: solid-connection/solid-connect-server
Length of output: 2848
🏁 Script executed:
Repository: solid-connection/solid-connect-server
Length of output: 13808
불필요한 DB 조회 제거 및 null 안전성 처리가 필요합니다.
아래 두 가지 개선 사항을 적용하세요:
미사용 엔티티 조회 제거 (라인 25):
Post post = postRepository.getById(postId);로 조회한 엔티티가 이후에 사용되지 않습니다. 불필요한 DB 쿼리이므로 제거하세요.Null 안전성 처리 (라인 26–27):
getAndDeleteViewCount(key)는 Redis 키가 만료되거나 존재하지 않을 경우 null을 반환할 수 있습니다. 이 값이 null일 때increaseViewCount()에 전달되면 SQL 쿼리가 예상치 못한 동작을 할 수 있으므로, 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
🤖 Prompt for AI Agents