Skip to content

Commit f4002ff

Browse files
authored
Merge pull request #287 from Podo-Store/develop
feat/chore: 조회수 중복 카운팅 삭제 & 배포 설정 수정
2 parents e724d04 + 2af5436 commit f4002ff

File tree

4 files changed

+9
-49
lines changed

4 files changed

+9
-49
lines changed

docker/docker-compose.green.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ services:
2020
- "6381:6379"
2121
networks:
2222
default:
23+
external: true
2324
name: green-network
2425

scripts/deploy.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ cd /home/ubuntu/app
66
# 환경변수 DOCKER_APP_NAME을 spring으로 설정
77
DOCKER_APP_NAME=spring
88

9+
# 네트워크 존재 여부 확인 및 생성
10+
NETWORK_NAME="app-network"
11+
12+
if ! sudo docker network ls | grep -q "$NETWORK_NAME"; then
13+
echo "Docker 네트워크가 없습니다. $NETWORK_NAME를 생성합니다."
14+
sudo docker network create $NETWORK_NAME
15+
fi
916

1017
# 실행중인 blue가 있는지 확인
1118
# 프로젝트의 실행 중인 컨테이너를 확인하고, 해당 컨테이너가 실행 중인지 여부를 EXIST_BLUE 변수에 저장

src/main/java/PodoeMarket/podoemarket/product/controller/ProductController.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@ public ResponseEntity<?> scriptInfo(@AuthenticationPrincipal UserEntity userInfo
8181
final int likeCount = productService.getLikeCount(productId);
8282

8383
// 조회수 증가
84-
if (userInfo != null) // 로그인
85-
viewCountService.incrementViewForLogged(userInfo.getId(), productId);
86-
else // 비로그인
87-
viewCountService.incrementViewForProduct(productId);
84+
viewCountService.incrementViewForProduct(productId);
8885

8986
// 총 조회수
9087
long viewCount = viewCountService.getProductViewCount(productId);

src/main/java/PodoeMarket/podoemarket/service/ViewCountService.java

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
import org.springframework.scheduling.annotation.Scheduled;
1010
import org.springframework.stereotype.Service;
1111

12-
import java.time.Duration;
13-
import java.time.LocalDate;
14-
import java.time.LocalDateTime;
15-
import java.time.format.DateTimeFormatter;
1612
import java.util.Set;
1713
import java.util.UUID;
1814

@@ -23,27 +19,6 @@ public class ViewCountService {
2319
private final StringRedisTemplate redisTemplate;
2420
private final ProductRepository productRepo;
2521

26-
// 로그인한 사용자의 조회수 증가 처리
27-
public void incrementViewForLogged(UUID userId, UUID productId) {
28-
final String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
29-
final String key = String.format("user:%s:views:%s", userId.toString(), today);
30-
31-
// 이미 조회한 상품인지 확인 (NullPointerException 방지)
32-
Long result = redisTemplate.opsForSet().add(key, productId.toString());
33-
boolean isView = result != null && result == 1;
34-
35-
// 키 만료 시간 설정(당일 자정까지)
36-
LocalDateTime midnight = LocalDate.now().plusDays(1).atStartOfDay();
37-
Duration timeUntilMidnight = Duration.between(LocalDateTime.now(), midnight);
38-
redisTemplate.expire(key, timeUntilMidnight);
39-
40-
if(isView) {
41-
// 조회수 증가
42-
String viewCountkey = "product:views:" + productId;
43-
redisTemplate.opsForValue().increment(viewCountkey);
44-
}
45-
}
46-
4722
// 비로그인 사용자의 조회 확인 (쿠키는 컨트롤러에서 처리)
4823
public void incrementViewForProduct(UUID productId) {
4924
final String viewCountKey = "product:views:" + productId.toString();
@@ -110,26 +85,6 @@ public void syncViewCountFromRedis() {
11085
}
11186
}
11287

113-
// 오래된 사용자 조회 기록 삭제 (30일 이상 된 기록)
114-
Set<String> oldUserViewKeys = redisTemplate.keys("user:*:views:*");
115-
if (oldUserViewKeys != null && !oldUserViewKeys.isEmpty()) {
116-
for (String key : oldUserViewKeys) {
117-
String[] parts = key.split(":");
118-
119-
if (parts.length >= 4) {
120-
String dateStr = parts[3];
121-
try {
122-
LocalDate keyDate = LocalDate.parse(dateStr, DateTimeFormatter.ofPattern("yyyyMMdd"));
123-
124-
if (keyDate.isBefore(LocalDate.now().minusDays(30)))
125-
redisTemplate.delete(key);
126-
} catch (Exception e) {
127-
log.warn("날짜 파싱 오류: {}", key);
128-
}
129-
}
130-
}
131-
}
132-
13388
log.info("조회수 동기화 및 데이터 정리 성공");
13489
} catch (Exception e) {
13590
log.error("작품 조회수 동기화 중 오류 발생: ", e);

0 commit comments

Comments
 (0)