Skip to content

Commit 5b6d45c

Browse files
committed
Added caching mechanism and cache duration flag to openmeteo backend.
1 parent e0113bf commit 5b6d45c

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

backends/open-meteo.com.go

+42-11
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"io"
88
"log"
99
"net/http"
10+
"os"
1011
"regexp"
1112
"strings"
12-
"sync"
1313
"time"
1414

1515
"github.com/schachmat/wego/iface"
@@ -119,7 +119,39 @@ func (opmeteo *openmeteoConfig) Setup() {
119119
flag.StringVar(&opmeteo.apiKey, "openmeteo-api-key", "", "openmeteo backend: the api `KEY` to use if commercial usage")
120120
flag.BoolVar(&opmeteo.debug, "openmeteo-debug", false, "openmeteo backend: print raw requests and responses")
121121
opmeteo.cache = make(map[string]cachedData) // initializing a cache
122-
opmeteo.cacheDuration = 1 * time.Hour // setting the cache duation to 1 hour
122+
flag.DurationVar(&opmeteo.cacheDuration, "openmeteo-cache-duration", 1*time.Hour, "openmeteo backend: duration for caching weather data") // adding the cache duration flag
123+
opmeteo.LoadCacheFromDisk()
124+
}
125+
126+
func (opmeteo *openmeteoConfig) SaveCacheToDisk() {
127+
file, err := os.Create("/tmp/wego_cache.json")
128+
129+
if err != nil {
130+
log.Fatal(err)
131+
}
132+
133+
defer file.Close()
134+
135+
json.NewEncoder(file).Encode(opmeteo.cache)
136+
}
137+
138+
func (opmeteo *openmeteoConfig) LoadCacheFromDisk() {
139+
file, err := os.Open("/tmp/wego_cache.json")
140+
141+
if os.IsNotExist(err) {
142+
log.Println("Cache file not found")
143+
return
144+
}
145+
146+
if err != nil {
147+
log.Fatal(err)
148+
}
149+
150+
defer file.Close()
151+
152+
if err := json.NewDecoder(file).Decode(&opmeteo.cache); err != nil {
153+
log.Println("Failed to load cache from disk, starting with an empty cache:", err)
154+
}
123155
}
124156

125157
func (opmeteo *openmeteoConfig) parseDaily(dailyInfo Hourly) []iface.Day {
@@ -171,21 +203,17 @@ func parseCurCond(current curCond) (ret iface.Cond) {
171203

172204
}
173205

174-
var mu sync.Mutex
175-
176206
func (opmeteo *openmeteoConfig) Fetch(location string, numdays int) iface.Data {
177207
var ret iface.Data
178208
var params []string
179209
var loc string
180210

181211
//checking cache before fething the data
182-
mu.Lock()
183212
if cached, ok := opmeteo.cache[location]; ok {
184213
if time.Since(cached.Timestamp) < opmeteo.cacheDuration {
185214
return cached.Data
186215
}
187216
}
188-
mu.Unlock()
189217

190218
if numdays <= 0 {
191219
log.Fatal("Number of days less than 1 ")
@@ -207,9 +235,11 @@ func (opmeteo *openmeteoConfig) Fetch(location string, numdays int) iface.Data {
207235

208236
res, err := http.Get(requri)
209237
if err != nil {
210-
log.Fatal("Unable to get weather data: ", err)
238+
log.Println("Unable to get weather data: ", err)
239+
return ret
211240
} else if res.StatusCode != 200 {
212-
log.Fatal("Unable to get weather data: http status ", res.StatusCode)
241+
log.Println("Unable to get weather data: http status ", res.StatusCode)
242+
return ret
213243
}
214244
defer res.Body.Close()
215245

@@ -226,7 +256,8 @@ func (opmeteo *openmeteoConfig) Fetch(location string, numdays int) iface.Data {
226256

227257
var resp openmeteoResponse
228258
if err = json.Unmarshal(body, &resp); err != nil {
229-
log.Println(err)
259+
log.Println("Failed to unmarshal weather data:", err)
260+
return ret
230261
}
231262

232263
ret.Current = parseCurCond(resp.Current)
@@ -240,12 +271,12 @@ func (opmeteo *openmeteoConfig) Fetch(location string, numdays int) iface.Data {
240271
ret.Forecast = forecast
241272
}
242273

243-
mu.Lock()
244274
opmeteo.cache[location] = cachedData{
245275
Data: ret, //store the fetched data in the cache
246276
Timestamp: time.Now(), // store the current time in the cache
247277
}
248-
mu.Unlock()
278+
279+
opmeteo.SaveCacheToDisk()
249280

250281
return ret
251282
}

0 commit comments

Comments
 (0)