A Faraday middleware that respects HTTP cache
A Faraday middleware that respects HTTP cache
A Faraday middleware that respects HTTP cache, by checking expiration and validation of the stored responses.
Add it to your Gemfile:
gem 'faraday-http-cache'You have to use the middleware in the Faraday instance that you want to,
along with a suitable store to cache the responses. You can use the new
shortcut using a symbol or passing the middleware class
client = Faraday.new do |builder|
builder.use :http_cache, store: Rails.cache
# or
builder.use Faraday::HttpCache, store: Rails.cache
builder.adapter Faraday.default_adapter
endThe middleware accepts a store option for the cache backend responsible for recording
the API responses that should be stored. Stores should respond to write, read and delete,
just like an object from the ActiveSupport::Cache API.
# Connect the middleware to a Memcache instance.
store = ActiveSupport::Cache.lookup_store(:mem_cache_store, ['localhost:11211'])
client = Faraday.new do |builder|
builder.use :http_cache, store: store
builder.adapter Faraday.default_adapter
end
# Or use the Rails.cache instance inside your Rails app.
client = Faraday.new do |builder|
builder.use :http_cache, store: Rails.cache
builder.adapter Faraday.default_adapter
endThe default store provided is a simple in memory cache that lives on the client instance. This type of store might not be persisted across multiple processes or connection instances so it is probably not suitable for most production environments. Make sure that you configure a store that is suitable for you.
The stdlib JSON module is used for serialization by default, which can struggle with unicode
characters in responses in Ruby will be cached
## License
Copyright (c) 2012-2018 Plataformatec.
Copyright (c) 2019 SourceLevel and contributors.
[marshal]: https://www.ruby-doc.org/core-3.0/Marshal.htmlNo open issues yet, or sync has not completed.