Skip to content
Snippets Groups Projects
Commit 2da7dfe8 authored by Alex Reisner's avatar Alex Reisner
Browse files

Refactor: add Geocoder::Cache class.

This allows us to add a method for expiring cache keys and generally
gives us more control over the cache interface.
parent 09bb77f7
No related branches found
No related tags found
No related merge requests found
require "geocoder/configuration" require "geocoder/configuration"
require "geocoder/calculations" require "geocoder/calculations"
require "geocoder/cache"
require "geocoder/railtie" require "geocoder/railtie"
require "geocoder/request" require "geocoder/request"
......
module Geocoder
##
# The working Cache object, or +nil+ if none configured.
#
def self.cache
if @cache.nil? and store = Geocoder::Configuration.cache
@cache = Cache.new(store, Geocoder::Configuration.cache_prefix)
end
@cache
end
class Cache
def initialize(store, prefix)
@store = store
@prefix = prefix
end
##
# Read from the Cache.
#
def [](url)
interpret store[key_for(url)]
end
##
# Write to the Cache.
#
def []=(url, value)
store[key_for(url)] = value
end
##
# Expire cache entry for given URL,
# or pass <tt>:all</tt> to expire everything.
#
def expire(url)
if url == :all
urls.each{ |u| expire(u) }
else
self[url] = nil
end
end
private # ----------------------------------------------------------------
attr_reader :prefix, :store
##
# Cache key for a given URL.
#
def key_for(url)
[prefix, url].join
end
##
# Array of keys with the currently configured prefix
# that have non-nil values.
#
def keys
store.keys.select{ |k| k.match /^#{prefix}/ and interpret(store[k]) }
end
##
# Array of cached URLs.
#
def urls
keys.map{ |k| k[/^#{prefix}(.*)/, 1] }
end
##
# Clean up value before returning. Namely, convert empty string to nil.
# (Some key/value stores return empty string instead of nil.)
#
def interpret(value)
value == "" ? nil : value
end
end
end
...@@ -60,7 +60,7 @@ module Geocoder ...@@ -60,7 +60,7 @@ module Geocoder
rescue TimeoutError rescue TimeoutError
warn "Geocoding API not responding fast enough " + warn "Geocoding API not responding fast enough " +
"(see Geocoder::Configuration.timeout to set limit)." "(see Geocoder::Configuration.timeout to set limit)."
end end
end end
## ##
...@@ -82,13 +82,12 @@ module Geocoder ...@@ -82,13 +82,12 @@ module Geocoder
# Fetches a raw search result (JSON string). # Fetches a raw search result (JSON string).
# #
def fetch_raw_data(query, reverse = false) def fetch_raw_data(query, reverse = false)
url = query_url(query, reverse)
key = cache_key(url)
timeout(Geocoder::Configuration.timeout) do timeout(Geocoder::Configuration.timeout) do
unless cache and (response = cache[key]) and response != "" url = query_url(query, reverse)
unless cache and response = cache[url]
response = Net::HTTP.get_response(URI.parse(url)).body response = Net::HTTP.get_response(URI.parse(url)).body
if cache if cache
cache[key] = response cache[url] = response
end end
end end
response response
...@@ -96,24 +95,10 @@ module Geocoder ...@@ -96,24 +95,10 @@ module Geocoder
end end
## ##
# Cache key for a given URL. # The working Cache object.
#
def cache_key(url)
[cache_prefix, url].join
end
##
# The configured prefix for cache keys.
#
def cache_prefix
Geocoder::Configuration.cache_prefix || "geocoder:"
end
##
# The configured cache store.
# #
def cache def cache
Geocoder::Configuration.cache Geocoder.cache
end end
## ##
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment