Skip to content

Commit

Permalink
Always use the null cache
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasleese committed Aug 23, 2018
1 parent 6d1576d commit c84a024
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 192 deletions.
10 changes: 1 addition & 9 deletions lib/gds_api/json_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,7 @@ def initialize(options = {})
end

@logger = options[:logger] || NullLogger.instance
disable_cache = options[:disable_cache] || ENV.fetch("GDS_API_DISABLE_CACHE", false)

if disable_cache || options[:cache_size]&.zero?
@cache = NullCache.new
else
cache_size = options[:cache_size] || DEFAULT_CACHE_SIZE
cache_ttl = options[:cache_ttl] || DEFAULT_CACHE_TTL
@cache = JsonClient.cache(cache_size, cache_ttl)
end
@cache = NullCache.new
@options = options
end

Expand Down
4 changes: 2 additions & 2 deletions test/gds_api_base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ def test_should_accept_options_as_second_arg
def test_setting_cache_size_from_options
GdsApi::JsonClient.cache = false
api = ConcreteApi.new("https://foo", cache_size: 2)
assert_equal 2, api.client.cache.max_size
assert api.client.cache.is_a? GdsApi::NullCache
end

def test_setting_cache_size_from_default_options
GdsApi::JsonClient.cache = false
GdsApi::Base.default_options = { cache_size: 4 }
api = ConcreteApi.new("http://bar")
assert_equal 4, api.client.cache.max_size
assert api.client.cache.is_a? GdsApi::NullCache
end

def test_disabling_cache
Expand Down
181 changes: 0 additions & 181 deletions test/json_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,94 +90,6 @@ def test_should_fetch_and_parse_json_into_response
assert_equal GdsApi::Response, @client.get_json(url).class
end

def test_should_cache_multiple_requests_to_same_url_across_instances
url = "http://some.endpoint/some.json"
result = { "foo" => "bar" }
stub_request(:get, url).to_return(body: JSON.dump(result), status: 200)
response_a = GdsApi::JsonClient.new.get_json(url)
response_b = GdsApi::JsonClient.new.get_json(url)
assert_equal response_a.to_hash, response_b.to_hash
assert_requested :get, url, times: 1
end

def test_allow_overriding_the_number_of_cached_items
# Clear out the default cache instance, because otherwise the customisation
# doesn't take effect, due to some non-obvious behaviour in JsonClient.
GdsApi::JsonClient.cache = nil

url = "http://some.endpoint/"

stub_request(:get, %r{\A#{url}}).to_return do |request|
{ body: { "url" => request.uri }.to_json, status: 200 }
end

response_a = GdsApi::JsonClient.new(cache_size: 5).get_json("#{url}/first.json")
response_b = GdsApi::JsonClient.new.get_json("#{url}/second.json")
4.times { |n| GdsApi::JsonClient.new.get_json("#{url}/#{n}.json") }

response_c = GdsApi::JsonClient.new.get_json("#{url}/second.json")
response_d = GdsApi::JsonClient.new.get_json("#{url}/first.json")

assert_requested :get, "#{url}/second.json", times: 1
assert_requested :get, "#{url}/first.json", times: 2
assert_equal response_b.to_hash, response_c.to_hash
assert_equal response_a.to_hash, response_d.to_hash
end

def test_should_cache_requests_for_15_mins_by_default
url = "http://some.endpoint/some.json"
result = { "foo" => "bar" }
stub_request(:get, url).to_return(body: JSON.dump(result), status: 200)
response_a = GdsApi::JsonClient.new.get_json(url)
response_b = GdsApi::JsonClient.new.get_json(url)

assert_requested :get, url, times: 1
assert_equal response_a.to_hash, response_b.to_hash

Timecop.travel(15 * 60 - 30) do # now + 14 mins 30 secs
response_c = GdsApi::JsonClient.new.get_json(url)

assert_requested :get, url, times: 1
assert_equal response_a.to_hash, response_c.to_hash
end

Timecop.travel(15 * 60 + 30) do # now + 15 mins 30 secs
response_d = GdsApi::JsonClient.new.get_json(url)

assert_requested :get, url, times: 2
assert_equal response_a.to_hash, response_d.to_hash
end
end

def test_should_allow_overriding_cache_ttl
# Clear out the default cache instance, because otherwise the customisation
# doesn't take effect, due to some non-obvious behaviour in JsonClient.
GdsApi::JsonClient.cache = nil

url = "http://some.endpoint/some.json"
result = { "foo" => "bar" }
stub_request(:get, url).to_return(body: JSON.dump(result), status: 200)
response_a = GdsApi::JsonClient.new(cache_ttl: 5 * 60).get_json(url)
response_b = GdsApi::JsonClient.new.get_json(url)

assert_requested :get, url, times: 1
assert_equal response_a.to_hash, response_b.to_hash

Timecop.travel(5 * 60 - 30) do # now + 4 mins 30 secs
response_c = GdsApi::JsonClient.new.get_json(url)

assert_requested :get, url, times: 1
assert_equal response_a.to_hash, response_c.to_hash
end

Timecop.travel(5 * 60 + 30) do # now + 5 mins 30 secs
response_d = GdsApi::JsonClient.new.get_json(url)

assert_requested :get, url, times: 2
assert_equal response_a.to_hash, response_d.to_hash
end
end

def test_should_allow_disabling_caching
url = "http://some.endpoint/some.json"
result = { "foo" => "bar" }
Expand Down Expand Up @@ -215,77 +127,6 @@ def test_should_allow_disabling_caching_via_environment_variable
ENV.delete("GDS_API_DISABLE_CACHE")
end

def test_should_respect_expiry_headers
url = "http://some.endpoint/some.json"
result = { "foo" => "bar" }
stub_request(:get, url).to_return(
body: JSON.dump(result),
status: 200,
headers: { "Expires" => (Time.now + 7 * 60).utc.httpdate }
)

response_a = GdsApi::JsonClient.new.get_json(url)

Timecop.travel(7 * 60 - 30) do # now + 6 mins 30 secs
response_b = GdsApi::JsonClient.new.get_json(url)

assert_requested :get, url, times: 1
assert_equal response_a.to_hash, response_b.to_hash
end

Timecop.travel(7 * 60 + 30) do # now + 7 mins 30 secs
response_c = GdsApi::JsonClient.new.get_json(url)

assert_requested :get, url, times: 2
assert_equal response_a.to_hash, response_c.to_hash
end
end

def test_should_respect_cache_control_headers_with_max_age
url = "http://some.endpoint/max_age.json"
result = { "foo" => "bar" }
stub_request(:get, url).to_return(
body: JSON.dump(result),
status: 200,
headers: { "Cache-Control" => "max-age=420, public" } # 7 minutes
)

response_a = GdsApi::JsonClient.new.get_json(url)

Timecop.travel(7 * 60 - 30) do # now + 6 mins 30 secs
response_b = GdsApi::JsonClient.new.get_json(url)

assert_requested :get, url, times: 1
assert_equal response_a.to_hash, response_b.to_hash
end

Timecop.travel(7 * 60 + 30) do # now + 7 mins 30 secs
response_c = GdsApi::JsonClient.new.get_json(url)

assert_requested :get, url, times: 2
assert_equal response_a.to_hash, response_c.to_hash
end
end

def test_should_respect_cache_control_headers_with_no_cache
url = "http://some.endpoint/no_cache.json"
result = { "foo" => "bar" }
stub_request(:get, url).to_return(
body: JSON.dump(result),
status: 200,
headers: { "Cache-Control" => "no-cache, public" }
)

response_a = GdsApi::JsonClient.new.get_json(url)

Timecop.travel(7 * 60 - 30) do # now + 6 mins 30 secs
response_b = GdsApi::JsonClient.new.get_json(url)

assert_requested :get, url, times: 2
assert_equal response_a.to_hash, response_b.to_hash
end
end

def test_does_not_cache_responses_with_cache_control_private
url = "http://some.endpoint/private.json"
result = { "foo" => "bar" }
Expand Down Expand Up @@ -365,28 +206,6 @@ def test_should_use_cache_control_headers_over_expires_headers
end
end

def test_should_fallback_to_expires_headers_if_cache_control_is_malformed
url = "http://some.endpoint/url.json"
result = { "foo" => "bar" }
stub_request(:get, url).to_return(
body: JSON.dump(result),
status: 200,
headers: {
"Cache-Control" => "foo, bar, baz",
"Expires" => (Time.now + 7 * 60).utc.httpdate
}
)

response_a = GdsApi::JsonClient.new.get_json(url)

Timecop.travel(7 * 60 - 30) do # now + 6 mins 30 secs
response_b = GdsApi::JsonClient.new.get_json(url)

assert_requested :get, url, times: 1
assert_equal response_a.to_hash, response_b.to_hash
end
end

def test_get_bang_should_raise_http_not_found_if_404_returned_from_endpoint
url = "http://some.endpoint/some.json"
stub_request(:get, url).to_return(body: "{}", status: 404)
Expand Down

0 comments on commit c84a024

Please sign in to comment.