-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[refactor] currentPriceRepository 개선 (#435)
* rename: 메서드명을 fetchCurrentPrice로 변경 * refactor: 현재가 조회 모듈 리팩토링 * refactor: redis key, value 메서드 추가 * rename: currentPriceRepository 클래스명 변경 * rename: CurrentPriceRedisRepository의 메소드를 인터페이스에 맞게 변경
- Loading branch information
1 parent
236fffb
commit 8959077
Showing
38 changed files
with
219 additions
and
194 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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
54 changes: 54 additions & 0 deletions
54
src/main/java/codesquad/fineants/domain/kis/repository/CurrentPriceRedisRepository.java
This file contains 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,54 @@ | ||
package codesquad.fineants.domain.kis.repository; | ||
|
||
import static codesquad.fineants.domain.kis.service.KisService.*; | ||
|
||
import java.util.Arrays; | ||
import java.util.Optional; | ||
|
||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import codesquad.fineants.domain.common.money.Money; | ||
import codesquad.fineants.domain.kis.client.KisClient; | ||
import codesquad.fineants.domain.kis.client.KisCurrentPrice; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
@Repository | ||
public class CurrentPriceRedisRepository implements PriceRepository { | ||
private static final String CURRENT_PRICE_FORMAT = "cp:%s"; | ||
private final RedisTemplate<String, String> redisTemplate; | ||
private final KisClient kisClient; | ||
|
||
@Override | ||
public void savePrice(KisCurrentPrice... currentPrices) { | ||
Arrays.stream(currentPrices).forEach(this::savePrice); | ||
} | ||
|
||
private KisCurrentPrice savePrice(KisCurrentPrice currentPrice) { | ||
redisTemplate.opsForValue().set(currentPrice.toRedisKey(CURRENT_PRICE_FORMAT), currentPrice.toRedisValue()); | ||
return currentPrice; | ||
} | ||
|
||
@Override | ||
public Optional<Money> fetchPriceBy(String tickerSymbol) { | ||
Optional<String> currentPrice = getCachedPrice(tickerSymbol); | ||
if (currentPrice.isEmpty()) { | ||
Optional<KisCurrentPrice> kisCurrentPrice = fetchAndCachePriceFromKis(tickerSymbol); | ||
return kisCurrentPrice | ||
.map(KisCurrentPrice::getPrice) | ||
.map(Money::won); | ||
} | ||
return currentPrice.map(Money::won); | ||
} | ||
|
||
private Optional<String> getCachedPrice(String tickerSymbol) { | ||
return Optional.ofNullable(redisTemplate.opsForValue().get(String.format(CURRENT_PRICE_FORMAT, tickerSymbol))); | ||
} | ||
|
||
private Optional<KisCurrentPrice> fetchAndCachePriceFromKis(String tickerSymbol) { | ||
return kisClient.fetchCurrentPrice(tickerSymbol) | ||
.blockOptional(TIMEOUT) | ||
.map(this::savePrice); | ||
} | ||
} |
50 changes: 0 additions & 50 deletions
50
src/main/java/codesquad/fineants/domain/kis/repository/CurrentPriceRepository.java
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
src/main/java/codesquad/fineants/domain/kis/repository/PriceRepository.java
This file contains 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 codesquad.fineants.domain.kis.repository; | ||
|
||
import java.util.Optional; | ||
|
||
import codesquad.fineants.domain.common.money.Money; | ||
import codesquad.fineants.domain.kis.client.KisCurrentPrice; | ||
|
||
public interface PriceRepository { | ||
void savePrice(KisCurrentPrice... currentPrices); | ||
|
||
Optional<Money> fetchPriceBy(String tickerSymbol); | ||
} |
This file contains 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 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 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 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.