實現了 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 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!