-
Notifications
You must be signed in to change notification settings - Fork 34
[volume-9] Product Ranking with Redis #217
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5952d70
feat: 10λΆ λ¨μ μ§κ³ μΆκ°
sieun0322 38414d9
feat: redis μ§κ³ λ° DB μ μ₯ λ°°μΉ
sieun0322 c2b5e1e
feat: λνΉ μ‘°ν API μΆκ°
sieun0322 973d137
feat: score carrry over λ°°μΉ μΆκ°
sieun0322 a5c564f
Merge pull request #8 from sieun0322/feature/week9-what-is-popularity
sieun0322 684de80
fix: coderabbitai 리뷰 μμ
sieun0322 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
apps/commerce-api/src/main/java/com/loopers/application/product/ProductListItem.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package com.loopers.application.product; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| public record ProductListItem(Long id, String name, BigDecimal price, long likeCount, Integer rank) { | ||
| public static ProductListItem from(Long id, String name, BigDecimal price, long likeCount, Integer rank) { | ||
| return new ProductListItem( | ||
| id, | ||
| name, | ||
| price, | ||
| likeCount, | ||
| rank | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 0 additions & 30 deletions
30
apps/commerce-api/src/main/java/com/loopers/application/product/ProductWithLikeCount.java
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
apps/commerce-api/src/main/java/com/loopers/application/ranking/RankInfo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.loopers.application.ranking; | ||
|
|
||
| public record RankInfo(Integer rank, Double score) { | ||
|
|
||
| public static RankInfo of(Integer rank, Double score) { | ||
| return new RankInfo(rank, score); | ||
| } | ||
|
|
||
| public static RankInfo empty() { | ||
| return new RankInfo(null, null); | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
apps/commerce-api/src/main/java/com/loopers/application/ranking/RankingFacade.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package com.loopers.application.ranking; | ||
|
|
||
| import com.loopers.application.product.ProductQueryService; | ||
| import com.loopers.application.product.ProductListItem; | ||
| import com.loopers.interfaces.api.product.ProductV1Dto; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.PageImpl; | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.redis.core.RedisTemplate; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Component | ||
| public class RankingFacade { | ||
|
|
||
| private final RedisTemplate<String, String> redisTemplate; | ||
| private final ProductQueryService productQueryService; | ||
| private final RankingService rankingService; | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public Page<ProductListItem> getProductRankings(Long userId, String date, int size, int page) { | ||
| String rankingKey = "ranking:all:" + date; | ||
|
|
||
| // Redisμμ λνΉ λ°μ΄ν° μ‘°ν (νμ΄μ§ μ²λ¦¬) | ||
| long start = (long) (page - 1) * size; | ||
| long end = start + size - 1; | ||
|
|
||
| Set<String> rankedProductIds = redisTemplate.opsForZSet().reverseRange(rankingKey, start, end); | ||
|
|
||
| if (rankedProductIds == null || rankedProductIds.isEmpty()) { | ||
| return new PageImpl<>(List.of(), PageRequest.of(page - 1, size), 0); | ||
| } | ||
|
|
||
| // productIds 리μ€νΈ μμ± | ||
| List<Long> productIds = rankedProductIds.stream() | ||
| .map(Long::parseLong) | ||
| .toList(); | ||
|
|
||
| // μν μ 보 μΌκ΄ μ‘°ν (μ’μμ μ 보 ν¬ν¨) | ||
| List<ProductListItem> productsWithLike = productQueryService.getProductListByProductIds(userId, productIds); | ||
| Long totalCount = rankingService.getTotalRankingCount(date); | ||
|
|
||
| return new PageImpl<>(productsWithLike, PageRequest.of(page - 1, size), totalCount); | ||
| } | ||
| } | ||
62 changes: 62 additions & 0 deletions
62
apps/commerce-api/src/main/java/com/loopers/application/ranking/RankingService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package com.loopers.application.ranking; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.redis.core.RedisTemplate; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.format.DateTimeFormatter; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Service | ||
| public class RankingService { | ||
|
|
||
| private final RedisTemplate<String, String> redisTemplate; | ||
|
|
||
| public Integer getProductRank(Long productId) { | ||
| String date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); | ||
| String rankingKey = "ranking:all:" + date; | ||
| Long rank = redisTemplate.opsForZSet().reverseRank(rankingKey, productId.toString()); | ||
|
|
||
| // rankκ° nullμ΄λ©΄ λνΉμ μλ μν | ||
| return rank != null ? rank.intValue() + 1 : null; // 0λΆν° μμνλ―λ‘ +1 | ||
| } | ||
|
|
||
| /** | ||
| * νΉμ λ μ§μ μ 체 λνΉμμ μνμ μμλ₯Ό μ‘°ν | ||
| * | ||
| * @param date μ‘°νν λ μ§ (yyyyMMdd νμ) | ||
| * @param productId μν ID | ||
| * @return λνΉ μμ (1λΆν° μμ), λνΉμ μμΌλ©΄ null | ||
| */ | ||
| public Integer getProductRank(String date, Long productId) { | ||
| String rankingKey = "ranking:all:" + date; | ||
| Long rank = redisTemplate.opsForZSet().reverseRank(rankingKey, productId.toString()); | ||
|
|
||
| // rankκ° nullμ΄λ©΄ λνΉμ μλ μν | ||
| return rank != null ? rank.intValue() + 1 : null; // 0λΆν° μμνλ―λ‘ +1 | ||
| } | ||
|
|
||
| /** | ||
| * νΉμ λ μ§μ μ 체 λνΉμμ μνμ μ μλ₯Ό μ‘°ν | ||
| * | ||
| * @param date μ‘°νν λ μ§ (yyyyMMdd νμ) | ||
| * @param productId μν ID | ||
| * @return λνΉ μ μ, λνΉμ μμΌλ©΄ null | ||
| */ | ||
| public Double getProductScore(String date, Long productId) { | ||
| String rankingKey = "ranking:all:" + date; | ||
| return redisTemplate.opsForZSet().score(rankingKey, productId.toString()); | ||
| } | ||
|
|
||
| /** | ||
| * νΉμ λ μ§μ μ 체 λνΉ μ΄ κ°μ μ‘°ν | ||
| * | ||
| * @param date μ‘°νν λ μ§ (yyyyMMdd νμ) | ||
| * @return λνΉμ μλ μ΄ μν κ°μ | ||
| */ | ||
| public Long getTotalRankingCount(String date) { | ||
| String rankingKey = "ranking:all:" + date; | ||
| return redisTemplate.opsForZSet().zCard(rankingKey); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: Loopers-dev-lab/loopers-spring-java-template
Length of output: 263
π Script executed:
rg -n "getProductListByProductIds" --type java -A 20Repository: Loopers-dev-lab/loopers-spring-java-template
Length of output: 6530
π Script executed:
rg -n "reverseRange" --type java -A 10 -B 5Repository: Loopers-dev-lab/loopers-spring-java-template
Length of output: 4054
π Script executed:
cat -n apps/commerce-api/src/main/java/com/loopers/application/ranking/RankingFacade.java | head -50Repository: Loopers-dev-lab/loopers-spring-java-template
Length of output: 2287
π Script executed:
rg -n "class RankingFacade" --type java -A 50Repository: Loopers-dev-lab/loopers-spring-java-template
Length of output: 4009
π Script executed:
rg -n "LinkedHashSet" --type java apps/commerce-apiRepository: Loopers-dev-lab/loopers-spring-java-template
Length of output: 70
π Script executed:
rg -n "rankedProductIds" --type java -B 2 -A 5Repository: Loopers-dev-lab/loopers-spring-java-template
Length of output: 1933
Redis
reverseRangeμμ λ°νλ μμκ° λ³΄μ‘΄λμ§ μμ΅λλ€.Set<String> rankedProductIds = redisTemplate.opsForZSet().reverseRange(...)μμ λ°νλ Setμ μμλ₯Ό 보μ₯νμ§ μλ 컬λ μ μ΄λ―λ‘, μ΄λ₯Ό Listλ‘ λ³ννλ©΄ Redisμ μ μ λ΄λ¦Όμ°¨μμ΄ μμ€λ©λλ€. κ²°κ³Όμ μΌλ‘ API μλ΅μ μμκ° λνΉ μμμ μΌμΉνμ§ μμ μ μμ΅λλ€.λμ
reverseRangeWithScores()λλ κ²°κ³Όλ₯ΌLinkedHashSetμΌλ‘ λ°μμ μμλ₯Ό μ μ§νκ±°λ, λ€μ μ λ ¬νλ λ°©μμΌλ‘ μμ ν΄μΌ ν©λλ€.π€ Prompt for AI Agents