FaradayHttpCache是一个Faraday的扩展,实现了HTTP的缓存。通过检查过期时间来验证已存储的响应信息。
使用方法:
client = Faraday.new do |builder| builder.use :http_cache, store: Rails.cache # or builder.use Faraday::HttpCache, store: Rails.cache builder.adapter Faraday.default_adapterend使用Memcached:
# 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_adapterend# 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_adapterend日志:
client = Faraday.new do |builder| builder.use :http_cache, store: Rails.cache, logger: Rails.logger builder.adapter Faraday.default_adapterendclient.get('https://site/api/users')# logs "HTTP Cache: [GET users] miss, store"
评论