Skip to content

Instantly share code, notes, and snippets.

@tkosuga
Last active December 16, 2015 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkosuga/5517667 to your computer and use it in GitHub Desktop.
Save tkosuga/5517667 to your computer and use it in GitHub Desktop.
lib/flickraw/api.rbをlib/flickraw/api_ext.rbな感じでcache。キャッシュ先でMarshal.dump/loadしてます。
# lib/flickraw/api.rb
def call(req, args={}, &block)
rest_path = FlickRaw.secure ? REST_PATH_SECURE : REST_PATH
http_response = @oauth_consumer.post_form(rest_path, @access_secret, {:oauth_token => @access_token}, build_args(args, req))
process_response(req, http_response.body)
end
# lib/flickraw/api.rb
def cache_key(req, args)
URI.encode((req + args.to_s).gsub(/[\s\t=>'",\{\}]/, "/").gsub(/\/+/, "/"))
end
def call(req, args={}, &block)
cache_key = cache_key(req, args)
result = CacheServer.instance.get(cache_key)
if result.blank?
rest_path = FlickRaw.secure ? REST_PATH_SECURE : REST_PATH
http_response = @oauth_consumer.post_form(rest_path, @access_secret, {:oauth_token => @access_token}, build_args(args, req))
result = process_response(req, http_response.body)
CacheServer.instance.push(cache_key, result)
end
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment