Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache for abbreviated city names #9

Merged
merged 2 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add cache for abbreviated city names
  • Loading branch information
krisukox committed Jul 21, 2023
commit 03eee1bee6c9ac8bc3bcafa56ea929e2657281c3
6 changes: 6 additions & 0 deletions flights/city.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func decodeInnerObject(body *bufio.Reader) ([][][][]interface{}, error) {
}

func (s *Session) AbbrCity(city string, lang language.Tag) (string, error) {
if abbrCity, ok := s.Cities.Load(city); ok {
return abbrCity, nil
}

resp, err := s.doRequestCity(city, lang)
if err != nil {
return "", err
Expand Down Expand Up @@ -95,6 +99,8 @@ func (s *Session) AbbrCity(city string, lang language.Tag) (string, error) {
return "", fmt.Errorf("the requested city name didn't match the found. requested: %s found: %s", city, foundCity)
}

s.Cities.Store(city, serializedCity)

return serializedCity, nil
}

Expand Down
2 changes: 1 addition & 1 deletion flights/city_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestAbbrCity(t *testing.T) {
)

session := &Session{
httpClientMock,
client: httpClientMock,
}

cityA := "Athens"
Expand Down
2 changes: 1 addition & 1 deletion flights/price_graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestGetPriceGraph(t *testing.T) {
)

session := &Session{
httpClientMock,
client: httpClientMock,
}

offers, err := session.GetPriceGraph(time.Now().AddDate(0, 0, 2), time.Now().AddDate(0, 0, 5), 0, "Athens", "Warsaw", currency.PLN, language.English)
Expand Down
22 changes: 20 additions & 2 deletions flights/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,37 @@ package flights

import (
"net/http"
"sync"
"time"
)

type Client interface {
type Map[K comparable, V any] struct {
m sync.Map
}

func (m *Map[K, V]) Load(key K) (value V, ok bool) {
v, ok := m.m.Load(key)
if !ok {
return value, ok
}
return v.(V), ok
}

func (m *Map[K, V]) Store(key K, value V) { m.m.Store(key, value) }

type HttpClient interface {
Do(req *http.Request) (*http.Response, error)
}

type Session struct {
client Client
Cities Map[string, string]

client HttpClient
}

func New() *Session {
return &Session{
Cities: Map[string, string]{},
client: &http.Client{
Timeout: 30 * time.Second,
},
Expand Down