summaryrefslogtreecommitdiffstats
path: root/nws/nws.go
diff options
context:
space:
mode:
authorsiddharth ravikumar <s@ricketyspace.net>2022-06-18 03:10:00 -0400
committersiddharth ravikumar <s@ricketyspace.net>2022-06-18 03:10:00 -0400
commit8b8f6d4adc105a8249157842512e3f90e8aa3190 (patch)
tree23f8c65f8b5c5db80151c89eef104fcf123e45e0 /nws/nws.go
parent8082d625c130e7037a73846b1f80145327b26d14 (diff)
nws: update `GetAlerts`
Implement caching.
Diffstat (limited to 'nws/nws.go')
-rw-r--r--nws/nws.go19
1 files changed, 15 insertions, 4 deletions
diff --git a/nws/nws.go b/nws/nws.go
index a1fd4ef..7caecc9 100644
--- a/nws/nws.go
+++ b/nws/nws.go
@@ -92,6 +92,7 @@ type ForecastBundle struct {
var pCache *cache.Cache
var fCache *cache.Cache
var fhCache *cache.Cache
+var aCache *cache.Cache
var baseUrl *url.URL
func init() {
@@ -100,6 +101,7 @@ func init() {
pCache = cache.NewCache()
fCache = cache.NewCache()
fhCache = cache.NewCache()
+ aCache = cache.NewCache()
// Parse NWS base url.
baseUrl, err = url.Parse("https://api.weather.gov")
@@ -270,19 +272,28 @@ func GetAlerts(lat, lng float32) (fc *FeatureCollection, err *Error) {
return
}
+ // Point: lat,lng.
+ ll := fmt.Sprintf("%.4f,%.4f", lat, lng)
+
// Build query.
q := url.Values{}
q.Add("status", "actual")
q.Add("message_type", "alert")
- q.Add("point", fmt.Sprintf("%.4f,%.4f", lat, lng))
+ q.Add("point", ll)
q.Add("urgency", "Immediate,Expected")
q.Add("certainty", "Observed,Likely,Possible")
u.RawQuery = q.Encode()
// Hit it.
- body, _, err := get(u.String())
- if err != nil {
- return
+ var expires time.Time
+ var body []byte
+ if body = aCache.Get(ll); len(body) == 0 {
+ body, expires, err = get(u.String())
+ if err != nil {
+ return
+ }
+ // Cache it.
+ aCache.Set(ll, body, expires)
}
// Unmarshal.