-
Notifications
You must be signed in to change notification settings - Fork 205
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 write_options
to be specified for FaradayMiddleware::Caching
#155
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,9 @@ class Caching < Faraday::Middleware | |
# :ignore_params - String name or Array names of query params | ||
# that should be ignored when forming the cache | ||
# key (default: []). | ||
# :write_options - Hash of settings that should be passed as the third | ||
# options parameter to the cache's #write method. If not | ||
# specified, no options parameter will be passed. | ||
# | ||
# Yields if no cache is given. The block should return a cache object. | ||
def initialize(app, cache = nil, options = {}) | ||
|
@@ -48,7 +51,11 @@ def call(env) | |
response = @app.call(env) | ||
|
||
if CACHEABLE_STATUS_CODES.include?(response.status) | ||
cache.write(key, response) | ||
if @options[:write_options] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added this |
||
cache.write(key, response, @options[:write_options]) | ||
else | ||
cache.write(key, response) | ||
end | ||
end | ||
end | ||
finalize_response(response, env) | ||
|
@@ -81,7 +88,11 @@ def cache_on_complete(env) | |
# response.status is nil at this point, any checks need to be done inside on_complete block | ||
@app.call(env).on_complete do |response_env| | ||
if CACHEABLE_STATUS_CODES.include?(response_env.status) | ||
cache.write(key, response_env.response) | ||
if @options[:write_options] | ||
cache.write(key, response_env.response, @options[:write_options]) | ||
else | ||
cache.write(key, response_env.response) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines 90-95 are exactly the same as lines 53-58, the only difference is the variable they use: |
||
end | ||
response_env | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did consider other names, but this felt like the best one, being the most explicit about where exactly these options go