Skip to content

Commit 011682b

Browse files
committed
feat: Zset, TTL, key 정책 적용
1 parent 982af43 commit 011682b

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.loopers.ranking;
2+
3+
import java.time.Duration;
4+
import java.time.LocalDate;
5+
import java.time.ZoneId;
6+
import java.time.ZonedDateTime;
7+
import java.time.format.DateTimeFormatter;
8+
import org.springframework.stereotype.Component;
9+
import org.springframework.util.StringUtils;
10+
11+
@Component
12+
public class RankingKeyGenerator {
13+
14+
private static final String KEY_PREFIX = "ranking:all:";
15+
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
16+
private static final ZoneId ZONE_ID = ZoneId.of("Asia/Seoul");
17+
private static final Duration TTL = Duration.ofDays(2);
18+
19+
public String resolve(ZonedDateTime dateTime) {
20+
ZonedDateTime target = dateTime != null ? dateTime : ZonedDateTime.now(ZONE_ID);
21+
return KEY_PREFIX + FORMATTER.format(target);
22+
}
23+
24+
public String resolve(LocalDate date) {
25+
LocalDate target = date != null ? date : LocalDate.now(ZONE_ID);
26+
return KEY_PREFIX + FORMATTER.format(target);
27+
}
28+
29+
public String resolve(String date) {
30+
LocalDate target = parseDate(date);
31+
return KEY_PREFIX + FORMATTER.format(target);
32+
}
33+
34+
public LocalDate parseDate(String date) {
35+
if (!StringUtils.hasText(date)) {
36+
return LocalDate.now(ZONE_ID);
37+
}
38+
return LocalDate.parse(date, FORMATTER);
39+
}
40+
41+
public Duration ttl() {
42+
return TTL;
43+
}
44+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.loopers.ranking;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class RankingScorePolicy {
7+
8+
private static final double VIEW_WEIGHT = 0.1;
9+
private static final double LIKE_WEIGHT = 0.2;
10+
private static final double ORDER_WEIGHT = 0.6;
11+
12+
public double viewScore() {
13+
return VIEW_WEIGHT;
14+
}
15+
16+
public double likeScore(long delta) {
17+
return delta * LIKE_WEIGHT;
18+
}
19+
20+
public double orderScore(Long price, Long quantity) {
21+
if (price == null || quantity == null || price <= 0 || quantity <= 0) {
22+
return 0d;
23+
}
24+
return price * quantity * ORDER_WEIGHT;
25+
}
26+
}

0 commit comments

Comments
 (0)