added caching to map results
All checks were successful
Run Integration Tests / test (push) Successful in 19s

This commit is contained in:
partisan 2024-12-05 19:47:53 +01:00
parent f2d9a37e87
commit 7498f4128a
3 changed files with 77 additions and 4 deletions

22
map.go
View file

@ -14,11 +14,20 @@ type NominatimResponse struct {
}
func geocodeQuery(query string) (latitude, longitude string, found bool, err error) {
// First, check if the result is in the cache
cachedLat, cachedLon, cachedFound, exists := geocodeCache.Get(query)
if exists {
printDebug("Geocode cache hit for query: %s", query)
return cachedLat, cachedLon, cachedFound, nil
}
printDebug("Geocode cache miss for query: %s", query)
// URL encode the query
query = url.QueryEscape(query)
queryEscaped := url.QueryEscape(query)
// Construct the request URL
urlString := fmt.Sprintf("https://nominatim.openstreetmap.org/search?format=json&q=%s", query)
urlString := fmt.Sprintf("https://nominatim.openstreetmap.org/search?format=json&q=%s", queryEscaped)
// Make the HTTP GET request
resp, err := http.Get(urlString)
@ -37,10 +46,15 @@ func geocodeQuery(query string) (latitude, longitude string, found bool, err err
if len(result) > 0 {
latitude = result[0].Lat
longitude = result[0].Lon
return latitude, longitude, true, nil
found = true
} else {
found = false
}
return "", "", false, nil
// Store the result in the cache
geocodeCache.Set(query, latitude, longitude, found)
return latitude, longitude, found, nil
}
func handleMapSearch(w http.ResponseWriter, settings UserSettings, query string) {