feat: add cache layer over api

This commit is contained in:
2026-02-08 21:26:39 +00:00
parent 3a18041c6f
commit 1eab9909e8
4 changed files with 133 additions and 1 deletions
+21 -1
View File
@@ -8,7 +8,7 @@ import (
"time"
)
func FetchLive() (TimezoneMap, error) {
func fetchLive() (TimezoneMap, error) {
client := http.Client{Timeout: 10 * time.Second}
// from: https://restcountries.com
@@ -32,6 +32,26 @@ func FetchLive() (TimezoneMap, error) {
return buildTzMap(apidData), nil
}
func Fetch() (TimezoneMap, error) {
// try to get from cache
tzMap, err := loadFromCache()
if err == nil {
return tzMap, nil
}
// fetch live and save
tzMap, err = fetchLive()
if err != nil {
return nil, err
}
if err = saveToCache(tzMap); err != nil {
return nil, err
}
return tzMap, nil
}
func buildTzMap(data []countryApiReponse) TimezoneMap {
tzMap := make(TimezoneMap)