-
Notifications
You must be signed in to change notification settings - Fork 1
[#23] pg수수료 ApplicationStartedEvent 로컬캐싱 전환 #28
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 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -1,118 +1,25 @@ | ||
| package com.commerce.platform.infrastructure.adaptor; | ||
|
|
||
| import com.commerce.platform.core.domain.enums.PayMethod; | ||
| import com.commerce.platform.core.domain.enums.PayProvider; | ||
| import com.commerce.platform.core.domain.enums.PgProvider; | ||
| import com.commerce.platform.infrastructure.persistence.PgFeeInfo; | ||
| import com.commerce.platform.infrastructure.persistence.PgFeeInfoRepository; | ||
| import jakarta.annotation.PostConstruct; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.data.redis.core.StringRedisTemplate; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * PG 라우팅을 위한 Redis 캐시 서비스 | ||
| * 수수료 낮은 순으로 정렬된 PG 목록 관리 | ||
| * 장애 PG는 제외 | ||
| * 장애 PG 캐싱 | ||
| */ | ||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class PgCacheService { | ||
|
|
||
| private final StringRedisTemplate redisTemplate; | ||
| private final PgFeeInfoRepository feeInfoRepository; | ||
|
|
||
| private static final String ROUTE_KEY_PREFIX = "pg:route:"; | ||
| private static final String HEALTH_KEY_PREFIX = "pg:health:"; | ||
|
|
||
| /** | ||
| * ZSet에 캐싱 확인 및 캐싱 | ||
| * key= pg:route:CARD:SHIN_HAN | ||
| * score: 수수료율 | ||
| */ | ||
| @PostConstruct | ||
| public void initPgCache() { | ||
| Set<String> keys = redisTemplate.keys(ROUTE_KEY_PREFIX + "*"); | ||
|
|
||
| if(!keys.isEmpty()) return; | ||
|
|
||
| // 전체 캐싱 | ||
| feeInfoRepository.findAllActiveAndValid() | ||
| .forEach(feeInfo -> { | ||
| String key = buildRouteKey(feeInfo.getPayMethod(), feeInfo.getPayProvider()); | ||
|
|
||
| redisTemplate.opsForZSet().add( | ||
| key, | ||
| feeInfo.getPgProvider().name(), | ||
| feeInfo.getFeeRate().doubleValue()); | ||
| }); | ||
| } | ||
|
|
||
| public PgProvider getBestPg(PayMethod payMethod, PayProvider payProvider, List<PgProvider> supportedPgs) { | ||
| // redis 조회 | ||
| Set<String> pgProviders = getAvailablePgsFromCache(payMethod, payProvider); | ||
|
|
||
| // miss | ||
| if (pgProviders == null || pgProviders.isEmpty()) { | ||
| pgProviders = refreshCache(payMethod, payProvider); | ||
| } | ||
|
|
||
| // 장애 PG 제외 첫번째 선택 | ||
| PgProvider bestPg = null; | ||
| for (String pgName : pgProviders) { | ||
| bestPg = PgProvider.getByPgName(pgName); | ||
| if (supportedPgs.contains(bestPg) && isHealthy(bestPg)) { | ||
| return bestPg; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * ZSet 수수료 asc | ||
| */ | ||
| private Set<String> getAvailablePgsFromCache(PayMethod payMethod, PayProvider payProvider) { | ||
| String key = buildRouteKey(payMethod, payProvider); | ||
| return redisTemplate.opsForZSet().range(key, 0, -1); | ||
| } | ||
|
|
||
| /** | ||
| * DB에서 수수료 조회 및 Redis 캐싱 | ||
| * ZSet score: 수수료율 | ||
| */ | ||
| public Set<String> refreshCache(PayMethod payMethod, PayProvider payProvider) { | ||
| String key = buildRouteKey(payMethod, payProvider); | ||
| // DB 조회: 수수료 낮은 순 | ||
| List<PgFeeInfo> configs = feeInfoRepository | ||
| .findByPayMethodAndPayProvider(payMethod, payProvider); | ||
|
|
||
| // todo 별도 스레드로 하는것이 좋을지 | ||
| // 기존 캐시 삭제 | ||
| redisTemplate.delete(key); | ||
|
|
||
| for (PgFeeInfo config : configs) { | ||
| redisTemplate.opsForZSet().add( | ||
| key, | ||
| config.getPgProvider().name(), | ||
| config.getFeeRate().doubleValue() | ||
| ); | ||
| } | ||
| private static final String HEALTH_KEY_PREFIX = "pg:health:"; | ||
|
|
||
| return configs.stream() | ||
| .sorted(Comparator.comparing(PgFeeInfo::getFeeRate)) | ||
| .map(pgFeeInfo -> pgFeeInfo.getPgProvider().name()) | ||
| .collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| /** | ||
| * PG 헬스 체크 | ||
| */ | ||
|
|
@@ -129,11 +36,4 @@ public void markPgAsUnhealthy(PgProvider pgProvider) { | |
| String healthKey = HEALTH_KEY_PREFIX + pgProvider.name(); | ||
| redisTemplate.opsForValue().set(healthKey, "ERROR", 30, TimeUnit.MINUTES); | ||
| } | ||
|
|
||
| /** | ||
| * Redis Key 생성: pg:route:CARD:SHIN_HAN | ||
| */ | ||
| private String buildRouteKey(PayMethod payMethod, PayProvider payProvider) { | ||
| return ROUTE_KEY_PREFIX + payMethod.name() + ":" + payProvider.name(); | ||
| } | ||
| } | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @f-lab-lyan 🙂 |
||
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.
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.
@f-lab-lyan 🙂
PostConstruct 는 싱글톤 생성 락에서 실행되어, 외부bean작업 포함되는게 좋지 않다고해서
ApplicationStartedEvent 시에 초기화하는 방향으로 수정해봤습니다.