Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to define its own set of Cacheable Status Code when using Faraday Middleware Cache #275

Merged
merged 6 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/faraday_middleware/response/caching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ def full_key?
@full_key ||= @options[:full_key]
end

def custom_status_codes
@custom_status_codes ||= CACHEABLE_STATUS_CODES & Array(@options[:cacheable_status_code]).map(&:to_i)
@custom_status_codes.any? ? @custom_status_codes : CACHEABLE_STATUS_CODES
Physium marked this conversation as resolved.
Show resolved Hide resolved
end

def cache_on_complete(env)
key = cache_key(env)
if (cached_response = cache.read(key))
Expand All @@ -101,7 +106,7 @@ def cache_on_complete(env)
end

def store_response_in_cache(key, response)
return unless CACHEABLE_STATUS_CODES.include?(response.status)
return unless custom_status_codes.include?(response.status)

if @options[:write_options]
cache.write(key, response, @options[:write_options])
Expand Down
38 changes: 38 additions & 0 deletions spec/unit/caching_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def fetch(key)
response = lambda { |_env|
[200, { 'Content-Type' => 'text/plain' }, "request:#{request_count += 1}"]
}
not_found = lambda { |_env|
[404, { 'Content-Type' => 'text/plain' }, "request:#{request_count += 1}"]
}
broken = lambda { |_env|
[500, { 'Content-Type' => 'text/plain' }, "request:#{request_count += 1}"]
}
Expand All @@ -54,6 +57,7 @@ def fetch(key)
stub.get('/broken', &broken)
stub.get('http://www.site-a.com/test', &response)
stub.get('http://www.site-b.com/test', &response)
stub.get('/not_found', &not_found)
end
end
end
Expand Down Expand Up @@ -95,6 +99,40 @@ def fetch(key)
expect(get('/broken').body).to eq('request:2')
end

context ':cacheable_status_code' do
let(:options) { { cacheable_status_code: %w[404] } }

it 'caches requests based on defined cacheable_status_code' do
expect(get('/').body).to eq('request:1')
expect(get('/not_found').body).to eq('request:2')
expect(get('/').body).to eq('request:3')
expect(get('/not_found').body).to eq('request:2')
end

context 'with invalid :cacheable_status_code status' do
let(:options) { { cacheable_status_code: %w[404,500] } }

it 'caches requests based on valid defined cacheable_status_code' do
expect(get('/not_found').body).to eq('request:1')
expect(get('/broken').body).to eq('request:2')
expect(get('/not_found').body).to eq('request:1')
expect(get('/broken').body).to eq('request:3')
end
end

context 'with no valid :cacheable_status_code status' do
let(:options) { { cacheable_status_code: %w[500] } }
it 'caches requests based on default cacheable_status_code' do
expect(get('/').body).to eq('request:1')
expect(get('/broken').body).to eq('request:2')
expect(get('/').body).to eq('request:1')
expect(get('/not_found').body).to eq('request:3')
expect(get('/not_found').body).to eq('request:3')
expect(get('/broken').body).to eq('request:4')
end
end
end

context ':ignore_params' do
let(:options) { { ignore_params: %w[utm_source utm_term] } }

Expand Down