This is a faraday middleware that integrates API Cache into the faraday middleware stack. Using Faraday API Cache, GET requests using Faraday will be cached automatically.
Make sure you have required the Faraday API Cache gem, or included it in your Gemfile. Then add Faraday API Cache as a request middleware:
farcon = Faraday.new(url: 'http://example.com') do |c|
c.request :api_cache
# more config goes here...
end
That's it!
Faraday API Cache passes any hash arguments to API Cache - see the API Cache documentation for more details (but the cache
parameter especially is useful).
Faraday API Cache also provides helper methods to change the logger and cache store used by API Cache:
FaradayAPICache.logger = Rails.logger
FaradayAPICache.store = Moneta.new(:Memcached)
There are already good middleware for caching in Faraday:
- faraday-http-cache takes cache settings from the expiry and modified response headers, whereas Faraday API Cache lets the developer set the cache expiry.
- The caching middleware in faraday_middleware is equivelent to Faraday API Cache, but if you're already using API Cache then it might make more sense to use Faraday API Cache.
Faraday API Cache was originally written by Ismael Celis, but was open sourced and is now maintained by Iain Beeston.