generated from Loopers-dev-lab/loop-pack-be-l2-vol2-java
-
Notifications
You must be signed in to change notification settings - Fork 44
[volume-5] 상품 목록 조회 성능, 좋아요 수 정렬 구조 개선 및 인덱스,캐시 적용 #197
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
Open
jsj1215
wants to merge
6
commits into
Loopers-dev-lab:jsj1215
Choose a base branch
from
jsj1215:jsj1215/volume-5
base: jsj1215
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
beca891
chore: .gitignore에 불필요한 파일 패턴 추가
jsj1215 da73b6f
merge: jsj1215/volume-4 브랜치를 main에 병합
jsj1215 a43ef44
refactor: 좋아요 서비스 책임 분리 및 Materialized View 도입
jsj1215 11dadc6
feat: 상품 캐시 시스템 구현 (Redis, Local Cache, WarmUp)
jsj1215 4438ac2
feat: 상품 API 엔드포인트 추가 및 JPA FK 최적화
jsj1215 fe3e3c3
test: 좋아요 카운트 동기화 테스트 보강 및 unlike HTTP 메서드 수정
jsj1215 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
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
42 changes: 42 additions & 0 deletions
42
apps/commerce-api/src/main/java/com/loopers/config/LocalCacheConfig.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,42 @@ | ||
| package com.loopers.config; | ||
|
|
||
| import com.github.benmanes.caffeine.cache.Caffeine; | ||
| import org.springframework.cache.CacheManager; | ||
| import org.springframework.cache.annotation.EnableCaching; | ||
| import org.springframework.cache.caffeine.CaffeineCache; | ||
| import org.springframework.cache.support.SimpleCacheManager; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.List; | ||
|
|
||
| @EnableCaching | ||
| @Configuration | ||
| public class LocalCacheConfig { | ||
|
|
||
| /** | ||
| * 로컬 캐시 사용 | ||
| * @return | ||
| */ | ||
| @Bean | ||
| public CacheManager cacheManager() { | ||
| SimpleCacheManager cacheManager = new SimpleCacheManager(); | ||
| cacheManager.setCaches(List.of( | ||
| buildCache("productSearch", Duration.ofMinutes(3), 1_000), // 상품 목록 조회용 | ||
| buildCache("productDetail", Duration.ofMinutes(5), 5_000) // 상품 상세 조회용 | ||
|
|
||
| // TTL의 경우, | ||
| // maxSize의 경우, 여러 개의 상품 상세 정보를 캐시하기 위해 상품 리스트 보다 상품 상세가 양이 더 많게 설정했음. | ||
| )); | ||
| return cacheManager; | ||
| } | ||
|
|
||
| private CaffeineCache buildCache(String name, Duration ttl, long maxSize) { | ||
| return new CaffeineCache(name, Caffeine.newBuilder() | ||
| .expireAfterWrite(ttl) | ||
| .maximumSize(maxSize) | ||
| .recordStats() | ||
| .build()); | ||
| } | ||
| } |
84 changes: 84 additions & 0 deletions
84
apps/commerce-api/src/main/java/com/loopers/config/ProductCacheWarmUp.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,84 @@ | ||
| package com.loopers.config; | ||
|
|
||
| import com.loopers.application.product.ProductDetailInfo; | ||
| import com.loopers.application.product.ProductFacade; | ||
| import com.loopers.application.product.ProductInfo; | ||
| import com.loopers.domain.product.ProductSearchCondition; | ||
| import com.loopers.domain.product.ProductSortType; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.boot.context.event.ApplicationReadyEvent; | ||
| import org.springframework.context.event.EventListener; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| /** | ||
| * 애플리케이션 기동 시 자주 조회되는 상품 데이터를 캐시에 미리 적재한다. | ||
| * | ||
| * - 상품 목록: 기본 정렬(LATEST) 0~2페이지 | ||
| * - 상품 상세: 목록 첫 페이지에 노출된 상품들의 상세 정보 | ||
| * | ||
| * Cache-Aside 로직은 ProductFacade가 담당하므로, | ||
| * 웜업은 Facade 메서드를 호출하여 자연스럽게 캐시를 적재한다. | ||
| * 웜업 실패 시에도 서비스 기동에는 영향을 주지 않는다. | ||
| */ | ||
| @Slf4j | ||
| @RequiredArgsConstructor | ||
| @Component | ||
| public class ProductCacheWarmUp { | ||
|
|
||
| private static final int WARM_UP_MAX_PAGE = 2; | ||
| private static final int DEFAULT_PAGE_SIZE = 20; | ||
|
|
||
| private final ProductFacade productFacade; | ||
|
|
||
| @EventListener(ApplicationReadyEvent.class) | ||
| public void warmUp() { | ||
| log.info("[CacheWarmUp] 상품 캐시 웜업 시작"); | ||
|
|
||
| int listCount = warmUpSearchCache(); | ||
| int detailCount = warmUpDetailCache(); | ||
|
|
||
| log.info("[CacheWarmUp] 상품 캐시 웜업 완료 - 목록 {}페이지, 상세 {}건", listCount, detailCount); | ||
| } | ||
|
|
||
| private int warmUpSearchCache() { | ||
| ProductSearchCondition defaultCondition = ProductSearchCondition.of(null, ProductSortType.LATEST, null); | ||
| int warmedPages = 0; | ||
|
|
||
| for (int page = 0; page <= WARM_UP_MAX_PAGE; page++) { | ||
| try { | ||
| Pageable pageable = PageRequest.of(page, DEFAULT_PAGE_SIZE); | ||
| productFacade.getProducts(defaultCondition, pageable); | ||
| warmedPages++; | ||
| } catch (Exception e) { | ||
| log.warn("[CacheWarmUp] 상품 목록 캐시 웜업 실패: page={}", page, e); | ||
| } | ||
| } | ||
| return warmedPages; | ||
| } | ||
|
|
||
| private int warmUpDetailCache() { | ||
| ProductSearchCondition defaultCondition = ProductSearchCondition.of(null, ProductSortType.LATEST, null); | ||
| int warmedCount = 0; | ||
|
|
||
| try { | ||
| Pageable pageable = PageRequest.of(0, DEFAULT_PAGE_SIZE); | ||
| Page<ProductInfo> products = productFacade.getProducts(defaultCondition, pageable); | ||
|
|
||
| for (ProductInfo productInfo : products.getContent()) { | ||
| try { | ||
| productFacade.getProduct(productInfo.id()); | ||
| warmedCount++; | ||
| } catch (Exception e) { | ||
| log.warn("[CacheWarmUp] 상품 상세 캐시 웜업 실패: productId={}", productInfo.id(), e); | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
| log.warn("[CacheWarmUp] 상품 상세 캐시 웜업 대상 조회 실패", e); | ||
| } | ||
| return warmedCount; | ||
| } | ||
| } |
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
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.
캐시 무효화를 트랜잭션 내부에서 직접 호출하는 것은 위험하다.
지금 방식은 커밋 전에 상세 캐시를 비워서 동시 조회가 이전 committed 데이터를 다시 캐시에 채울 수 있고, eviction 예외가 나면 상품 수정/삭제 자체의 가용성까지 같이 떨어뜨린다. 무효화는 커밋 이후 훅으로 분리하고, 실패는 로깅·재시도 대상으로 격리해서 DB 변경 성공 여부와 분리하는 편이 안전하다. 추가로 “커밋 전 동시 상세 조회가 stale cache를 재생성하지 않는다”와 “캐시 삭제 실패가 나도 DB 변경은 커밋된다” 통합 테스트를 넣어야 한다.
Also applies to: 69-70
🤖 Prompt for AI Agents