1

I use Faraday for request api. I want to cache response body and use it instead of request for specifed expired time.

I found FaradayMiddleware::Caching and I can cache response body, but I do not know how use cache instead of request.

API service which I use do not support http cache, so I can not use faraday-http-cache gem.

Below my code

connection = Faraday.new(url: 'http://exapmle.com') do |faraday|
  faraday.request :url_encoded
  cache_dir = File.join('/tmp/cache')
  faraday.response :caching do
    ActiveSupport::Cache::FileStore.new cache_dir, :expires_in => 3600
  end
  faraday.adapter Faraday.default_adapter
end

connection.get '/path/to/api', version: nil

How can I use cached data instead of normal response?

Cache key is request url, but url is dynamically change. So I think I need a way to get url which constructed by Faraday.

0 Answers0