From 4da78a9bab55aa814b6a9c71da7ed505f606f3e6 Mon Sep 17 00:00:00 2001 From: Xevion Date: Tue, 12 Dec 2023 03:11:00 -0600 Subject: [PATCH] Add map accessible cacheLocations --- api.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/api.go b/api.go index 4075b12..164f36a 100644 --- a/api.go +++ b/api.go @@ -97,8 +97,9 @@ type Location struct { } var ( - cachedLocations []Location - cacheExpiry time.Time + cachedLocations []Location + cachedLocationsMap map[uint]Location + cacheExpiry time.Time ) func init() { @@ -129,21 +130,25 @@ func GetLocations() []Location { return nil } - locations := make([]Location, 0, 150) + cachedLocations := make([]Location, 0, 150) doc.Find("input.property").Each(func(i int, s *goquery.Selection) { matches := parsePattern.FindStringSubmatch(s.Parent().Text()) id, _ := strconv.ParseUint(matches[3], 10, 32) - locations = append(locations, Location{ + cachedLocations = append(cachedLocations, Location{ id: uint(id), name: matches[1], address: matches[2], }) }) - cachedLocations = locations + // Build the map + cachedLocationsMap = make(map[uint]Location, len(cachedLocations)) + for _, location := range cachedLocations { + cachedLocationsMap[location.id] = location + } cacheExpiry = time.Now().Add(time.Hour * 3) - return locations + return cachedLocations }