Add map accessible cacheLocations

This commit is contained in:
2023-12-12 03:11:00 -06:00
parent 31d2daeaee
commit 4da78a9bab

17
api.go
View File

@@ -97,8 +97,9 @@ type Location struct {
} }
var ( var (
cachedLocations []Location cachedLocations []Location
cacheExpiry time.Time cachedLocationsMap map[uint]Location
cacheExpiry time.Time
) )
func init() { func init() {
@@ -129,21 +130,25 @@ func GetLocations() []Location {
return nil return nil
} }
locations := make([]Location, 0, 150) cachedLocations := make([]Location, 0, 150)
doc.Find("input.property").Each(func(i int, s *goquery.Selection) { doc.Find("input.property").Each(func(i int, s *goquery.Selection) {
matches := parsePattern.FindStringSubmatch(s.Parent().Text()) matches := parsePattern.FindStringSubmatch(s.Parent().Text())
id, _ := strconv.ParseUint(matches[3], 10, 32) id, _ := strconv.ParseUint(matches[3], 10, 32)
locations = append(locations, Location{ cachedLocations = append(cachedLocations, Location{
id: uint(id), id: uint(id),
name: matches[1], name: matches[1],
address: matches[2], 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) cacheExpiry = time.Now().Add(time.Hour * 3)
return locations return cachedLocations
} }