Remove caching from geocoder

This commit is contained in:
Eugene Burmakin 2025-09-03 19:56:38 +02:00
parent 690f80766e
commit 689b8cb0f1
3 changed files with 4 additions and 61 deletions

View file

@ -3,21 +3,15 @@
module LocationSearch
class GeocodingService
MAX_RESULTS = 10
CACHE_TTL = 1.hour
def initialize(query)
@query = query
@cache_key_prefix = 'location_search:geocoding'
end
def search
return [] if query.blank?
cache_key = "#{@cache_key_prefix}:#{Digest::SHA256.hexdigest(query.downcase)}"
Rails.cache.fetch(cache_key, expires_in: CACHE_TTL) do
perform_geocoding_search(query)
end
perform_geocoding_search(query)
rescue StandardError => e
Rails.logger.error "Geocoding search failed for query '#{query}': #{e.message}"
[]

View file

@ -13,20 +13,6 @@ module LocationSearch
end
def call
if coordinate_search?
return coordinate_based_search
else
return empty_result
end
end
private
def coordinate_search?
@latitude.present? && @longitude.present?
end
def coordinate_based_search
location = {
lat: @latitude,
lon: @longitude,
@ -36,13 +22,12 @@ module LocationSearch
find_matching_points([location])
end
private
def find_matching_points(geocoded_locations)
results = []
geocoded_locations.each do |location|
# Debug: Log the geocoded location
Rails.logger.info "LocationSearch: Searching for points near #{location[:name]} at [#{location[:lat]}, #{location[:lon]}]"
search_radius = @radius_override || determine_search_radius(location[:type])
matching_points = spatial_matcher.find_points_near(
@ -53,11 +38,7 @@ module LocationSearch
date_filter_options
)
# Debug: Log the number of matching points found
Rails.logger.info "LocationSearch: Found #{matching_points.length} points within #{search_radius}m radius"
if matching_points.empty?
# Try with a larger radius to see if there are any points nearby
wider_search = spatial_matcher.find_points_near(
@user,
location[:lat],
@ -65,7 +46,7 @@ module LocationSearch
1000, # 1km radius for debugging
date_filter_options
)
Rails.logger.info "LocationSearch: Found #{wider_search.length} points within 1000m radius (debug)"
next
end
@ -122,16 +103,5 @@ module LocationSearch
500 # Default radius for unknown types
end
end
def empty_result
{
locations: [],
total_locations: 0,
search_metadata: {
candidates_found: 0,
search_time_ms: 0
}
}
end
end
end

View file

@ -51,32 +51,11 @@ RSpec.describe LocationSearch::GeocodingService do
)
end
it 'caches results' do
expect(Rails.cache).to receive(:fetch).and_call_original
service.search
end
it 'limits results to MAX_RESULTS' do
expect(Geocoder).to receive(:search).with(query, limit: 10)
service.search
end
context 'with cached results' do
let(:cached_results) { [{ lat: 1.0, lon: 2.0, name: 'Cached' }] }
before do
allow(Rails.cache).to receive(:fetch).and_return(cached_results)
end
it 'returns cached results without calling Geocoder' do
expect(Geocoder).not_to receive(:search)
results = service.search
expect(results).to eq(cached_results)
end
end
end
context 'with blank query' do