Skip to content

Instantly share code, notes, and snippets.

@waynerobinson
Created September 25, 2012 02:10
Show Gist options
  • Save waynerobinson/3779570 to your computer and use it in GitHub Desktop.
Save waynerobinson/3779570 to your computer and use it in GitHub Desktop.
Cached Address Proxy
require "dalli"
require "yaml"
module MTDataStone
class CachableAddresses < Addresses
DALLI_HOST = "localhost:11211"
DALLI_OPTIONS = {}
EXPIRY_PERIOD = 60.days
def get_autocomplete_suggestions(fleet, street)
cached?("#{fleet}:#{street}") do
super(fleet, street)
end
end
def cached?(key, &block)
cached = fetch_cached(key)
unless cached
cached = yield
set_cached(key, cached)
end
cached
end
def cache
@cache ||= Dalli::Client.new(DALLI_HOST, DALLI_OPTIONS)
end
def fetch_cached(key)
data = cache.get(key)
return YAML.load(data) if data
end
def set_cached(key, value)
cache.set(key, cached.to_yaml, EXPIRY_PERIOD)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment