Created
September 25, 2012 02:10
-
-
Save waynerobinson/3779570 to your computer and use it in GitHub Desktop.
Cached Address Proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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