Faraday Http Cache
a Faraday middleware that respects HTTP cache, by checking expiration and validation of the stored responses.
Add it to your Gemfile:
Usage and configuration
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 end
The 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, :mem_cache_store, "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, Rails.cache builder.adapter Faraday.default_adapter end
The default store provided by ActiveSupport is the
MemoryStore one, so it's important to
configure a proper one for your production environment.
You can provide a
:logger option that will be receive debug informations based on the middleware
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"
See it live
You can clone this repository, install it's dependencies with Bundler (run
bundle install) and
examples/twitter.rb file to see a sample of the middleware usage - it's issuing
requests to the Twitter API and caching them, so the rate limit isn't reduced on every request by
the client object. After sleeping for 5 minutes the cache will expire and the client will hit the
Twitter API again.
Copyright (c) 2012 Plataformatec. See LICENSE file.