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 write_options to be specified for FaradayMiddleware::Caching #155

Merged
merged 2 commits into from
Jul 27, 2017
Merged

Allow write_options to be specified for FaradayMiddleware::Caching #155

merged 2 commits into from
Jul 27, 2017

Conversation

timrogers
Copy link
Contributor

@timrogers timrogers commented Jun 9, 2017

FaradayMiddleware::Caching is configured with a cache with responds to #read, #fetch and #write. Usually, this will be an instance of a class which conforms to the abstract cache store class ActiveSupport::Cache::Store.

In this pattern, the third argument of #write is an optional hash of options - for example, you can specify an expires_in to set how long until the cache key expires (this is the case where we're interested for a gem I maintain, https://github.com/ejholmes/restforce). Cache stores may support a range of options.

This allows you, when configuring the middleware, to specify a set of write_options which will be passed in as this third argument when writing to the cache.

The alternative to this would be passing in some kind of wrapper around your chosen cache store which does this for you, but it feels nicer for the middleware to do it.

`FaradayMiddleware::Caching` is configured with a cache with responds to `#read`, `#fetch` and `#write`. Usually, this will be an instance of a class which conforms to the abstract cache store class [`ActiveSupport::Cache::Store`](http://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html).

In this pattern, the third argument of `#write` is an optional hash of options - for example, you can specify an `expires_in` to set how long until the cache key expires. Cache stores may support a range of options.

This allows you, when configuring the middleware, to specify a set of `write_options` which will be passed in as this third argument when writing to the cache.

The alternative to this would be passing in some kind of wrapper around your chosen cache store which does this for you, but it feels nicer for the middleware to do it.
@@ -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]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added this if statement with backwards compatibility in mind - if people are using ActiveSupport::Cache::Store-confirming objects, this will work fine, but people may not be, especially in tests, so we shouldn't start passing options if the user hasn't set some.

@@ -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
Copy link
Contributor Author

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

@timrogers
Copy link
Contributor Author

@iMacTia Can you take a look at this? Looks like you've been active on this project most recently. Thanks ❤️

Copy link
Member

@iMacTia iMacTia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @timrogers for the PR.
I like the idea of this new feature and I appreciate the focus on backward compatibility.
I marked a quick refactor to make code a little DRYer 😄

cache.write(key, response_env.response, @options[:write_options])
else
cache.write(key, response_env.response)
end
Copy link
Member

Choose a reason for hiding this comment

The 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: response_env.response for the former, response for the latter (but they should be exactly the same).
Please refactor these lines into a method

@iMacTia iMacTia added this to the 0.12.0 milestone Jun 12, 2017
@timrogers
Copy link
Contributor Author

@iMacTia Done!

@iMacTia
Copy link
Member

iMacTia commented Jun 12, 2017

Great job, thanks @timrogers 👍
I've marked this for release 0.12 so will merge soon before that gets released

@timrogers
Copy link
Contributor Author

@iMacTia What is the planned release schedule for 0.12?

@iMacTia iMacTia merged commit 86a861d into lostisland:master Jul 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants