lib/geokit/geocoders.rb in geokit-with-google-premier-1.5.0 vs lib/geokit/geocoders.rb in geokit-with-google-premier-1.5.5
- old
+ new
@@ -471,10 +471,15 @@
# second (and subsequent) iterations, we push additional
# geolocs onto "geoloc.all"
geoloc.all.push(extracted_geoloc)
end
end
+
+ ## Cache the result
+ GeocodeCache.store(address, geoloc.lat, geoloc.lng)
+ logger.debug "Google geocoding CACHED. Address: #{address}. Place [#{geoloc.lat},#{geoloc.lng}]"
+
return geoloc
elsif doc.elements['//kml/Response/Status/code'].text == '620'
raise Geokit::TooManyQueriesError
else
logger.info "Google was unable to geocode address: "+address
@@ -600,10 +605,11 @@
res.country_code.chop!
res.success = !(res.city =~ /\(.+\)/)
res
end
end
+
# -------------------------------------------------------------------------------------------
# The Multi Geocoder
# -------------------------------------------------------------------------------------------
@@ -657,7 +663,32 @@
end
# If we get here, we failed completely.
GeoLoc.new
end
end
+
+ # -------------------------------------------------------------------------------------------
+ # Caching
+ # -------------------------------------------------------------------------------------------
+
+ class CacheGeocoder < Geocoder
+ private
+
+ def self.do_geocode(address, options = {})
+ logger.error("TRYING CACHE")
+
+ cache = GeocodeCache.find(:first, :conditions => ["address = ?",address])
+
+ if cache.nil?
+ logger.debug "Cache result for #{address} was nil."
+ GeoLoc.new
+ else
+ res = GeoLoc.new(:lat=>cache.lat, :lng=>cache.lng)
+ res.success = true
+ logger.debug "GeoCode result from cache. Address: #{address}. Place [#{cache.lat},#{cache.lng}]"
+ res
+ end
+ end
+ end
+
end
end