Skip to content

Commit 5fe3646

Browse files
committed
feat: 콜드 스타트 문제 해결을 위한 Score Carry-Over 스케줄러 추가
- `carryOverRanking` 메서드를 `RankingService`에 추가하여 랭킹 데이터 이월 지원 - 이월 작업 자동화를 위한 `RankingScheduler` 구현 및 Cron 스케줄 설정 - Redis 연산을 활용한 이월 데이터 처리 및 만료 시간 설정 로직 추가
1 parent 08f7cfe commit 5fe3646

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.loopers.application.rank;
2+
3+
import com.loopers.infrastructure.rank.RankingService;
4+
import java.time.LocalDateTime;
5+
import java.time.format.DateTimeFormatter;
6+
import lombok.RequiredArgsConstructor;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.springframework.scheduling.annotation.Scheduled;
9+
import org.springframework.stereotype.Component;
10+
11+
@Slf4j
12+
@Component
13+
@RequiredArgsConstructor
14+
public class RankingScheduler {
15+
16+
private final RankingService rankingService;
17+
18+
@Scheduled(cron = "0 50 23 * * *")
19+
public void scheduleRankingCarryOver() {
20+
LocalDateTime now = LocalDateTime.now();
21+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
22+
23+
String today = now.format(formatter);
24+
String tomorrow = now.plusDays(1).format(formatter);
25+
26+
log.info("Starting Ranking Carry-Over: {} -> {}", today, tomorrow);
27+
28+
try {
29+
rankingService.carryOverRanking(today, tomorrow, 0.1);
30+
log.info("Ranking Carry-Over completed successfully.");
31+
} catch (Exception e) {
32+
log.error("Ranking Carry-Over failed", e);
33+
}
34+
}
35+
}

apps/commerce-api/src/main/java/com/loopers/infrastructure/rank/RankingService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import java.time.format.DateTimeFormatter;
55
import java.util.List;
66
import java.util.Set;
7+
import java.util.concurrent.TimeUnit;
78
import lombok.RequiredArgsConstructor;
9+
import org.springframework.data.redis.connection.zset.Aggregate;
10+
import org.springframework.data.redis.connection.zset.Weights;
811
import org.springframework.data.redis.core.RedisTemplate;
912
import org.springframework.stereotype.Component;
1013

@@ -38,4 +41,14 @@ public Integer getProductRank(Long productId) {
3841

3942
return (rank != null) ? rank.intValue() + 1 : null;
4043
}
44+
45+
public void carryOverRanking(String sourceDate, String targetDate, double weight) {
46+
String sourceKey = "ranking:all:" + sourceDate;
47+
String targetKey = "ranking:all:" + targetDate;
48+
49+
redisTemplate.opsForZSet().unionAndStore(sourceKey, List.of(), targetKey,
50+
Aggregate.SUM, Weights.of(weight));
51+
52+
redisTemplate.expire(targetKey, 2, TimeUnit.DAYS);
53+
}
4154
}

0 commit comments

Comments
 (0)