aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Polden <mpolden@mpolden.no>2021-08-12 23:19:20 +0200
committerMartin Polden <mpolden@mpolden.no>2021-08-12 23:19:20 +0200
commite93621aac570728c57c6c24cf1a2890d505b943c (patch)
tree3c91bbf8dcde713bcab8d5c8d4c6e8a30e17922a
parentc2317c2a54af26ecbfe423241efca20910f5a2c9 (diff)
http: Cache each stop once
-rw-r--r--http/http.go45
-rw-r--r--http/types.go8
2 files changed, 32 insertions, 21 deletions
diff --git a/http/http.go b/http/http.go
index f8ecbe0..a87ba74 100644
--- a/http/http.go
+++ b/http/http.go
@@ -51,6 +51,25 @@ func urlPrefix(r *http.Request) string {
return url.String()
}
+func filterDepartures(departures []Departure, direction string) []Departure {
+ switch direction {
+ case inbound, outbound:
+ copy := make([]Departure, 0, len(departures))
+ for _, d := range departures {
+ towardsCentrum := *d.TowardsCentrum
+ if direction == inbound && !towardsCentrum {
+ continue
+ }
+ if direction == outbound && towardsCentrum {
+ continue
+ }
+ copy = append(copy, d)
+ }
+ return copy
+ }
+ return departures
+}
+
func (s *Server) getBusStops(urlPrefix string) (BusStops, bool, error) {
const cacheKey = "stops"
cached, hit := s.cache.Get(cacheKey)
@@ -98,23 +117,21 @@ func (s *Server) atbDepartures(urlPrefix string, nodeID int) (Departures, bool,
}
func (s *Server) enturDepartures(urlPrefix string, stopID int, direction string) (Departures, bool, error) {
- var cacheKey string
- if direction == inbound || direction == outbound {
- cacheKey = fmt.Sprintf("%d-%s", stopID, direction)
- } else {
- cacheKey = fmt.Sprintf("%d", stopID)
- }
+ cacheKey := strconv.Itoa(stopID)
cached, hit := s.cache.Get(cacheKey)
+ var departures Departures
if hit {
- return cached.(Departures), hit, nil
- }
- enturDepartures, err := s.Entur.Departures(stopID)
- if err != nil {
- return Departures{}, hit, err
+ departures = cached.(Departures)
+ } else {
+ enturDepartures, err := s.Entur.Departures(stopID)
+ if err != nil {
+ return Departures{}, hit, err
+ }
+ departures = convertDepartures(enturDepartures)
+ departures.URL = fmt.Sprintf("%s/api/v2/departures/%d", urlPrefix, stopID)
+ s.cache.Set(cacheKey, departures, s.ttl.departures)
}
- departures := convertDepartures(enturDepartures, direction)
- departures.URL = fmt.Sprintf("%s/api/v2/departures/%d", urlPrefix, stopID)
- s.cache.Set(cacheKey, departures, s.ttl.departures)
+ departures.Departures = filterDepartures(departures.Departures, direction)
return departures, hit, nil
}
diff --git a/http/types.go b/http/types.go
index 56855b0..2ed25e3 100644
--- a/http/types.go
+++ b/http/types.go
@@ -185,16 +185,10 @@ func convertForecasts(f atb.Forecasts) (Departures, error) {
}, nil
}
-func convertDepartures(enturDepartures []entur.Departure, direction string) Departures {
+func convertDepartures(enturDepartures []entur.Departure) Departures {
departures := make([]Departure, 0, len(enturDepartures))
const timeLayout = "2006-01-02T15:04:05.000"
for _, d := range enturDepartures {
- if direction == inbound && !d.Inbound {
- continue
- }
- if direction == outbound && d.Inbound {
- continue
- }
scheduledDepartureTime := d.ScheduledDepartureTime.Format(timeLayout)
registeredDepartureTime := ""
if !d.RegisteredDepartureTime.IsZero() {