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. You can use the new shortcut using a symbol or passing the middleware class
client = Faraday.new do |builder|
  builder.use :http_cache
  # or
  builder.use Faraday::HttpCache
  builder.adapter Faraday.default_adapter
endThe middleware uses the ActiveSupport::Cache API to record the responses from the targeted
endpoints, and any extra configuration option will be used to setup the cache store.
# Connect the middleware to a Memcache instance.
client = Faraday.new do |builder|
  builder.use :http_cache, store: :mem_cache_store, store_options: ['localhost:11211']
  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 by ActiveSupport is the MemoryStore one, so it's important to
configure a proper one for your production environment.
MultiJson is used for serialization by default. If you expect to be dealing with images, you can use Marshal instead.
client = Faraday.new do |builder|
  builder.use :http_cache, serializer: Marshal
  builder.adapter Faraday.default_adapter
endYou can provide a :logger option that will be receive debug informations based on the middleware
operations:
client = Faraday.new do |builder|
  builder.use :http_cache, logger: Rails.logger
  builder.adapter Faraday.default_adapter
end
client.get('http://site/api/users')
# logs "HTTP Cache: [GET users] miss, store"You can clone this repository, install it's dependencies with Bundler (run bundle install) and
execute the files under the examples directory to see a sample of the middleware usage.
The middleware will use the following headers to make caching decisions:
- Cache-Control
 - Age
 - Last-Modified
 - ETag
 - Expires
 
The max-age, must-revalidate, proxy-revalidate and s-maxage directives are checked.
Note: private caches are ignored.
Copyright (c) 2012-2013 Plataformatec. See LICENSE file.
