Skip to content

Commit

Permalink
fix: 시외버스 시간표 캐시확인로직 변경 (#457)
Browse files Browse the repository at this point in the history
(cherry picked from commit aed2df0)
  • Loading branch information
dradnats1012 authored and Choi-JJunho committed May 9, 2024
1 parent 9ef08ff commit 2636350
Showing 1 changed file with 7 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.Clock;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -45,7 +44,6 @@
import in.koreatech.koin.domain.bus.model.express.ExpressBusTimetable;
import in.koreatech.koin.domain.bus.model.express.OpenApiExpressBusArrival;
import in.koreatech.koin.domain.bus.repository.ExpressBusCacheRepository;
import in.koreatech.koin.domain.version.model.Version;
import in.koreatech.koin.domain.version.model.VersionType;
import in.koreatech.koin.domain.version.repository.VersionRepository;

Expand Down Expand Up @@ -101,7 +99,8 @@ public SingleBusTimeResponse searchBusTime(
}

public List<ExpressBusRemainTime> getBusRemainTime(BusStation depart, BusStation arrival) {
String busCacheId = ExpressBusCache.generateId(new ExpressBusRoute(depart.name().toLowerCase(), arrival.name().toLowerCase()));
String busCacheId = ExpressBusCache.generateId(
new ExpressBusRoute(depart.name().toLowerCase(), arrival.name().toLowerCase()));
if (!expressBusCacheRepository.existsById(busCacheId)) {
storeRemainTimeByOpenApi(depart.name().toLowerCase(), arrival.name().toLowerCase());
}
Expand Down Expand Up @@ -130,7 +129,7 @@ private void storeRemainTimeByOpenApi(String departName, String arrivalName) {
.toList()
);

if(!expressBusCache.getBusInfos().isEmpty()) {
if (!expressBusCache.getBusInfos().isEmpty()) {
expressBusCacheRepository.save(expressBusCache);
}

Expand All @@ -140,7 +139,7 @@ private void storeRemainTimeByOpenApi(String departName, String arrivalName) {
private JsonObject getBusApiResponse(String departName, String arrivalName) {
try {
URL url = getBusApiURL(departName, arrivalName);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-type", "application/json");
BufferedReader reader;
Expand Down Expand Up @@ -221,14 +220,7 @@ private List<ExpressBusRemainTime> getExpressBusRemainTime(
.toList();
}

public boolean isCacheExpired(Version version, Clock clock) {
Duration duration = Duration.between(version.getUpdatedAt().toLocalTime(), LocalTime.now(clock));
return duration.toSeconds() < 0
|| Duration.ofHours(ExpressBusCache.getCacheExpireHour()).toSeconds() <= duration.toSeconds();
}

public List<? extends BusTimetable> getExpressBusTimetable(String direction) {
Version version = versionRepository.getByType(VersionType.EXPRESS);
String depart = "";
String arrival = "";

Expand All @@ -240,15 +232,16 @@ public List<? extends BusTimetable> getExpressBusTimetable(String direction) {
depart = "terminal";
arrival = "koreatech";
}

if (depart.isEmpty() || arrival.isEmpty()) {
throw new UnsupportedOperationException();
}

if (isCacheExpired(version, clock)) {
String busCacheId = ExpressBusCache.generateId(new ExpressBusRoute(depart, arrival));
if (!expressBusCacheRepository.existsById(busCacheId)) {
storeRemainTimeByOpenApi(depart, arrival);
}

String busCacheId = ExpressBusCache.generateId(new ExpressBusRoute(depart, arrival));
ExpressBusCache expressBusCache = expressBusCacheRepository.getById(busCacheId);
if (Objects.isNull(expressBusCache)) {
return Collections.emptyList();
Expand Down

0 comments on commit 2636350

Please sign in to comment.