Add map accessible cacheLocations

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

13
api.go
View File

@@ -98,6 +98,7 @@ type Location struct {
var (
cachedLocations []Location
cachedLocationsMap map[uint]Location
cacheExpiry time.Time
)
@@ -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
}