实现了 HTTP 的缓存:Faraday Http Cache
Faraday Http Cache 是一个 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_adapter end
使用 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_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 end
日志:
client = Faraday.new do |builder| builder.use :http_cache, store: Rails.cache, logger: Rails.logger builder.adapter Faraday.default_adapter end client.get('http://site/api/users') # logs "HTTP Cache: [GET users] miss, store"
本文由用户 efbb 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!