Skip to content

Commit cf0af7a

Browse files
dradnats1012Choi-JJunho
authored andcommitted
fix: 시외버스 시간표 캐시확인로직 변경 (#457)
(cherry picked from commit aed2df0)
1 parent b8711b2 commit cf0af7a

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

src/main/java/in/koreatech/koin/domain/bus/util/ExpressBusOpenApiClient.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.net.HttpURLConnection;
1212
import java.net.URL;
1313
import java.time.Clock;
14-
import java.time.Duration;
1514
import java.time.LocalDateTime;
1615
import java.time.LocalTime;
1716
import java.util.ArrayList;
@@ -45,7 +44,6 @@
4544
import in.koreatech.koin.domain.bus.model.express.ExpressBusTimetable;
4645
import in.koreatech.koin.domain.bus.model.express.OpenApiExpressBusArrival;
4746
import in.koreatech.koin.domain.bus.repository.ExpressBusCacheRepository;
48-
import in.koreatech.koin.domain.version.model.Version;
4947
import in.koreatech.koin.domain.version.model.VersionType;
5048
import in.koreatech.koin.domain.version.repository.VersionRepository;
5149

@@ -101,7 +99,8 @@ public SingleBusTimeResponse searchBusTime(
10199
}
102100

103101
public List<ExpressBusRemainTime> getBusRemainTime(BusStation depart, BusStation arrival) {
104-
String busCacheId = ExpressBusCache.generateId(new ExpressBusRoute(depart.name().toLowerCase(), arrival.name().toLowerCase()));
102+
String busCacheId = ExpressBusCache.generateId(
103+
new ExpressBusRoute(depart.name().toLowerCase(), arrival.name().toLowerCase()));
105104
if (!expressBusCacheRepository.existsById(busCacheId)) {
106105
storeRemainTimeByOpenApi(depart.name().toLowerCase(), arrival.name().toLowerCase());
107106
}
@@ -130,7 +129,7 @@ private void storeRemainTimeByOpenApi(String departName, String arrivalName) {
130129
.toList()
131130
);
132131

133-
if(!expressBusCache.getBusInfos().isEmpty()) {
132+
if (!expressBusCache.getBusInfos().isEmpty()) {
134133
expressBusCacheRepository.save(expressBusCache);
135134
}
136135

@@ -140,7 +139,7 @@ private void storeRemainTimeByOpenApi(String departName, String arrivalName) {
140139
private JsonObject getBusApiResponse(String departName, String arrivalName) {
141140
try {
142141
URL url = getBusApiURL(departName, arrivalName);
143-
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
142+
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
144143
conn.setRequestMethod("GET");
145144
conn.setRequestProperty("Content-type", "application/json");
146145
BufferedReader reader;
@@ -221,14 +220,7 @@ private List<ExpressBusRemainTime> getExpressBusRemainTime(
221220
.toList();
222221
}
223222

224-
public boolean isCacheExpired(Version version, Clock clock) {
225-
Duration duration = Duration.between(version.getUpdatedAt().toLocalTime(), LocalTime.now(clock));
226-
return duration.toSeconds() < 0
227-
|| Duration.ofHours(ExpressBusCache.getCacheExpireHour()).toSeconds() <= duration.toSeconds();
228-
}
229-
230223
public List<? extends BusTimetable> getExpressBusTimetable(String direction) {
231-
Version version = versionRepository.getByType(VersionType.EXPRESS);
232224
String depart = "";
233225
String arrival = "";
234226

@@ -240,15 +232,16 @@ public List<? extends BusTimetable> getExpressBusTimetable(String direction) {
240232
depart = "terminal";
241233
arrival = "koreatech";
242234
}
235+
243236
if (depart.isEmpty() || arrival.isEmpty()) {
244237
throw new UnsupportedOperationException();
245238
}
246239

247-
if (isCacheExpired(version, clock)) {
240+
String busCacheId = ExpressBusCache.generateId(new ExpressBusRoute(depart, arrival));
241+
if (!expressBusCacheRepository.existsById(busCacheId)) {
248242
storeRemainTimeByOpenApi(depart, arrival);
249243
}
250244

251-
String busCacheId = ExpressBusCache.generateId(new ExpressBusRoute(depart, arrival));
252245
ExpressBusCache expressBusCache = expressBusCacheRepository.getById(busCacheId);
253246
if (Objects.isNull(expressBusCache)) {
254247
return Collections.emptyList();

0 commit comments

Comments
 (0)