Skip to content

Commit

Permalink
add $API_CLIENT_INVALIDATE_CACHE option to disable cache
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni committed Dec 16, 2023
1 parent 169afb7 commit c52d2e0
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions lib/ontologies_api_client/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'multi_json'
require 'digest'
require 'ostruct'

require 'benchmark'
##
# This monkeypatch makes OpenStruct act like Struct objects
class OpenStruct
Expand Down Expand Up @@ -48,7 +48,7 @@ def self.conn
rails = Kernel.const_get("Rails")
store = rails.cache if rails.cache
end
LinkedData::Client.config_connection(cache_store: store)
LinkedData::Client.config_connection(cache_store: store) unless $API_CLIENT_INVALIDATE_CACHE
end
LinkedData::Client.settings.conn
end
Expand All @@ -58,18 +58,20 @@ def self.get(path, params = {}, options = {})
raw = options[:raw] || false # return the unparsed body of the request
params = params.delete_if { |k, v| v == nil || v.to_s.empty? }
params[:ncbo_cache_buster] = Time.now.to_f if raw # raw requests don't get cached to ensure body is available
invalidate_cache = params.delete(:invalidate_cache) || false

invalidate_cache = params.delete(:invalidate_cache) || $API_CLIENT_INVALIDATE_CACHE || false
begin
puts "Getting: #{path} with #{params}" if $DEBUG_API_CLIENT
begin
response = conn.get do |req|
req.url path
req.params = params.dup
req.options[:timeout] = 60
req.headers.merge(headers)
req.headers[:invalidate_cache] = invalidate_cache
response = nil
time = Benchmark.realtime do
response = conn.get do |req|
req.url path
req.params = params.dup
req.options[:timeout] = 60
req.headers.merge(headers)
req.headers[:invalidate_cache] = invalidate_cache
end
end
puts "Getting: #{path} with #{params} (#{time}s)" if $DEBUG_API_CLIENT
rescue Exception => e
params = Faraday::Utils.build_query(params)
path << "?" unless params.empty? || path.include?("?")
Expand Down

0 comments on commit c52d2e0

Please sign in to comment.