lib/billy/cache.rb in puffing-billy-0.3.0 vs lib/billy/cache.rb in puffing-billy-0.4.0
- old
+ new
@@ -1,7 +1,7 @@
require 'resolv'
-require 'uri'
+require 'addressable/uri'
require 'yaml'
require 'billy/json_utils'
require 'singleton'
module Billy
@@ -70,26 +70,31 @@
@cache = {}
end
def key(method, orig_url, body, log_key = false)
ignore_params = Billy.config.ignore_params.include?(format_url(orig_url, true))
- url = URI(format_url(orig_url, ignore_params))
- key = method+'_'+url.host+'_'+Digest::SHA1.hexdigest(scope.to_s + url.to_s)
+ merge_cached_response_key = _merge_cached_response_key(orig_url)
+ url = Addressable::URI.parse(format_url(orig_url, ignore_params))
+ key = if merge_cached_response_key
+ method+'_'+Digest::SHA1.hexdigest(scope.to_s + merge_cached_response_key)
+ else
+ method+'_'+url.host+'_'+Digest::SHA1.hexdigest(scope.to_s + url.to_s)
+ end
body_msg = ''
- if method == 'post' and !ignore_params
+ if method == 'post' && !ignore_params && !merge_cached_response_key
body_formatted = JSONUtils::json?(body.to_s) ? JSONUtils::sort_json(body.to_s) : body.to_s
body_msg = " with body '#{body_formatted}'"
key += '_'+Digest::SHA1.hexdigest(body_formatted)
end
Billy.log(:info, "puffing-billy: CACHE KEY for '#{orig_url}#{body_msg}' is '#{key}'") if log_key
key
end
def format_url(url, ignore_params=false, dynamic_jsonp=Billy.config.dynamic_jsonp)
- url = URI(url)
+ url = Addressable::URI.parse(url)
port_to_include = Billy.config.ignore_cache_port ? '' : ":#{url.port}"
formatted_url = url.scheme+'://'+url.host+port_to_include+url.path
return formatted_url if ignore_params
@@ -130,9 +135,18 @@
def use_default_scope
scope_to nil
end
private
+
+ def _merge_cached_response_key(url)
+ Billy.config.merge_cached_responses_whitelist.each do |disable_regex|
+ if url =~ disable_regex
+ return disable_regex.to_s # Use the stringified regex as the cache key if it matches
+ end
+ end
+ nil
+ end
attr_writer :scope
end
end