11
11
import java .net .HttpURLConnection ;
12
12
import java .net .URL ;
13
13
import java .time .Clock ;
14
- import java .time .Duration ;
15
14
import java .time .LocalDateTime ;
16
15
import java .time .LocalTime ;
17
16
import java .util .ArrayList ;
45
44
import in .koreatech .koin .domain .bus .model .express .ExpressBusTimetable ;
46
45
import in .koreatech .koin .domain .bus .model .express .OpenApiExpressBusArrival ;
47
46
import in .koreatech .koin .domain .bus .repository .ExpressBusCacheRepository ;
48
- import in .koreatech .koin .domain .version .model .Version ;
49
47
import in .koreatech .koin .domain .version .model .VersionType ;
50
48
import in .koreatech .koin .domain .version .repository .VersionRepository ;
51
49
@@ -101,7 +99,8 @@ public SingleBusTimeResponse searchBusTime(
101
99
}
102
100
103
101
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 ()));
105
104
if (!expressBusCacheRepository .existsById (busCacheId )) {
106
105
storeRemainTimeByOpenApi (depart .name ().toLowerCase (), arrival .name ().toLowerCase ());
107
106
}
@@ -130,7 +129,7 @@ private void storeRemainTimeByOpenApi(String departName, String arrivalName) {
130
129
.toList ()
131
130
);
132
131
133
- if (!expressBusCache .getBusInfos ().isEmpty ()) {
132
+ if (!expressBusCache .getBusInfos ().isEmpty ()) {
134
133
expressBusCacheRepository .save (expressBusCache );
135
134
}
136
135
@@ -140,7 +139,7 @@ private void storeRemainTimeByOpenApi(String departName, String arrivalName) {
140
139
private JsonObject getBusApiResponse (String departName , String arrivalName ) {
141
140
try {
142
141
URL url = getBusApiURL (departName , arrivalName );
143
- HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
142
+ HttpURLConnection conn = (HttpURLConnection )url .openConnection ();
144
143
conn .setRequestMethod ("GET" );
145
144
conn .setRequestProperty ("Content-type" , "application/json" );
146
145
BufferedReader reader ;
@@ -221,14 +220,7 @@ private List<ExpressBusRemainTime> getExpressBusRemainTime(
221
220
.toList ();
222
221
}
223
222
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
-
230
223
public List <? extends BusTimetable > getExpressBusTimetable (String direction ) {
231
- Version version = versionRepository .getByType (VersionType .EXPRESS );
232
224
String depart = "" ;
233
225
String arrival = "" ;
234
226
@@ -240,15 +232,16 @@ public List<? extends BusTimetable> getExpressBusTimetable(String direction) {
240
232
depart = "terminal" ;
241
233
arrival = "koreatech" ;
242
234
}
235
+
243
236
if (depart .isEmpty () || arrival .isEmpty ()) {
244
237
throw new UnsupportedOperationException ();
245
238
}
246
239
247
- if (isCacheExpired (version , clock )) {
240
+ String busCacheId = ExpressBusCache .generateId (new ExpressBusRoute (depart , arrival ));
241
+ if (!expressBusCacheRepository .existsById (busCacheId )) {
248
242
storeRemainTimeByOpenApi (depart , arrival );
249
243
}
250
244
251
- String busCacheId = ExpressBusCache .generateId (new ExpressBusRoute (depart , arrival ));
252
245
ExpressBusCache expressBusCache = expressBusCacheRepository .getById (busCacheId );
253
246
if (Objects .isNull (expressBusCache )) {
254
247
return Collections .emptyList ();
0 commit comments