From e5314fd75d63d56911769f636fdd1d91bfbfaff3 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sun, 4 Apr 2021 00:22:35 +0200 Subject: Added 60 seconds caching to navitia api departures requests --- go.mod | 2 ++ go.sum | 8 +++++++- navitia_api_client/client.go | 10 ++++++++++ navitia_api_client/client_test.go | 1 + navitia_api_client/departures.go | 16 +++++++++++++++- navitia_api_client/departures_test.go | 9 +++++++++ 6 files changed, 44 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 6b55e17..c7ec694 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,8 @@ module git.adyxax.org/adyxax/trains go 1.16 require ( + github.com/kr/pretty v0.2.1 // indirect github.com/pkg/errors v0.9.1 + gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) diff --git a/go.sum b/go.sum index 51a261f..ba9b6cd 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,12 @@ +github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/navitia_api_client/client.go b/navitia_api_client/client.go index d08ca19..aef8d0e 100644 --- a/navitia_api_client/client.go +++ b/navitia_api_client/client.go @@ -3,12 +3,21 @@ package navitia_api_client import ( "fmt" "net/http" + "sync" "time" ) type Client struct { baseURL string httpClient *http.Client + + mutex sync.Mutex + cache map[string]cachedResult +} + +type cachedResult struct { + ts time.Time + result interface{} } func NewClient(token string) *Client { @@ -17,5 +26,6 @@ func NewClient(token string) *Client { httpClient: &http.Client{ Timeout: time.Minute, }, + cache: make(map[string]cachedResult), } } diff --git a/navitia_api_client/client_test.go b/navitia_api_client/client_test.go index ec51a06..332b222 100644 --- a/navitia_api_client/client_test.go +++ b/navitia_api_client/client_test.go @@ -13,6 +13,7 @@ func NewTestClient(ts *httptest.Server) *Client { return &Client{ baseURL: fmt.Sprintf(ts.URL), httpClient: ts.Client(), + cache: make(map[string]cachedResult), } } diff --git a/navitia_api_client/departures.go b/navitia_api_client/departures.go index a6dcf53..7738940 100644 --- a/navitia_api_client/departures.go +++ b/navitia_api_client/departures.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "net/http" + "time" ) type DeparturesResponse struct { @@ -43,7 +44,16 @@ type DeparturesResponse struct { } func (c *Client) GetDepartures() (departures *DeparturesResponse, err error) { - req, err := http.NewRequest("GET", fmt.Sprintf("%s/coverage/sncf/stop_areas/stop_area:SNCF:87723502/departures", c.baseURL), nil) + request := fmt.Sprintf("%s/coverage/sncf/stop_areas/stop_area:SNCF:87723502/departures", c.baseURL) + start := time.Now() + c.mutex.Lock() + defer c.mutex.Unlock() + if cachedResult, ok := c.cache[request]; ok { + if start.Sub(cachedResult.ts) < 60*1000*1000*1000 { + return cachedResult.result.(*DeparturesResponse), nil + } + } + req, err := http.NewRequest("GET", request, nil) if err != nil { return nil, err } @@ -55,5 +65,9 @@ func (c *Client) GetDepartures() (departures *DeparturesResponse, err error) { if err = json.NewDecoder(resp.Body).Decode(&departures); err != nil { return nil, err } + c.cache[request] = cachedResult{ + ts: start, + result: departures, + } return } diff --git a/navitia_api_client/departures_test.go b/navitia_api_client/departures_test.go index 2c87429..a1658d2 100644 --- a/navitia_api_client/departures_test.go +++ b/navitia_api_client/departures_test.go @@ -45,4 +45,13 @@ func TestGetDepartures(t *testing.T) { if len(departures.Departures) != 10 { t.Fatalf("did not decode normal-crepieux departures properly, got %d departures when expected 10", len(departures.Departures)) } + // test the cache (assuming the test takes less than 60 seconds (and it really should) it will be accurate) + ts.Close() + departures, err = client.GetDepartures() + if err != nil { + t.Fatalf("could not get normal-crepieux departures : %s", err) + } + if len(departures.Departures) != 10 { + t.Fatalf("did not decode normal-crepieux departures properly, got %d departures when expected 10", len(departures.Departures)) + } } -- cgit v1.2.3